TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 11-13-2008, 02:04 AM   #1 (permalink)
The Addict
 
tony's Avatar
 
Join Date: Aug 2008
Posts: 336
Thanks: 8
tony is on a distinguished road
Default How to access the method of an object that is inside an object

Hi guys and gals, once again i hit bump in this project.
the thing is that I have 2 classes, well I think it's better if i put the code first:

PHP Code:
<?php
class foo{
    private 
$a;
    private 
$b;
    
    function 
__construct(){
        
$this->a=3;
        
$this->b=3;
    }
    
    public function 
getA(){    return $this->a; }
    public function 
getB(){    return $this->b; }
    public function 
setA($t){ $this->a=$t; }
    public function 
setB($t){ $this->a=$t; }
}

clas bar{
    
$private c;
    
$private d;
    
    function 
__construct(){
        
$this->c=10;
        
$this->d= new foo;
    }
    
    public function 
getC(){ return $this->}
    public function 
getD(){ return $this->}
    public function 
setC($t){ $this->c=$t; }
    public function 
setD($t){ $this->d=$t; }
}
?>
now the problem rises when i want to get the getA() method from an instance of bar, like this:

PHP Code:
<?php
$house
=new Bar;
echo 
$house->getD()->getA(); // it should output 3 ideally 
?>
but I get this "Fatal error: Can't use method return value in write context"

any ideas on what i am doing wrong? thanks for the help in advance.
tony is offline  
Reply With Quote
Old 11-13-2008, 02:13 AM   #2 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

That returns 3 for me. Are you trying to do something like the following, because this is really the only way I can think of causing that error you're getting:

php Code:
if (empty($house->getD()->getA()))
{
   
}
Attached Files
File Type: txt Working-And-Formatted-Code.txt (832 Bytes, 146 views)
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Old 11-13-2008, 02:20 AM   #3 (permalink)
The Addict
 
tony's Avatar
 
Join Date: Aug 2008
Posts: 336
Thanks: 8
tony is on a distinguished road
Default

yes! i am trying to see if they are empty, I forgot to mention that.
tony is offline  
Reply With Quote
Old 11-13-2008, 02:26 AM   #4 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

I see, I see! Sadly, empty cannot check the return value, for some unknown reason. I don't know the real reasoning behind it. Assign the value to a variable first, and then check if that variable is empty or not.

php Code:
$iValue = $pHouse->getD()->getA();

if (empty($iValue))
{

}
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
The Following User Says Thank You to Wildhoney For This Useful Post:
tony (11-13-2008)
Old 11-13-2008, 02:43 AM   #5 (permalink)
The Addict
 
tony's Avatar
 
Join Date: Aug 2008
Posts: 336
Thanks: 8
tony is on a distinguished road
Default

Awesome, that worked! though I am curious as to why. I'll search more after I finish the project.
Thanks for the help :)
tony is offline  
Reply With Quote
Old 11-13-2008, 02:46 AM   #6 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

And please let us know, too! Best of luck, my friend
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 09:33 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design