noidtluom Posted September 29, 2007 Share Posted September 29, 2007 I've been using classes recently and I've learnt about $this->variable, and $this->function(). However, whilst learning MVC, I've seen some examples of $this->foo->function(); - Can anybody tell me what this is? I am assuming it is a type of "sub-function", but I'm not sure how to use it. Any help appreciated. Thanks in advance. Quote Link to comment https://forums.phpfreaks.com/topic/71140-solved-question-about-classes/ Share on other sites More sharing options...
leonglass Posted September 29, 2007 Share Posted September 29, 2007 That would refer to a member function of an object that is referenced through a variable of the calling object. class a { ... $foo = new b() ... } class b { ... function(){...} ... } Then at some point in class a code you could have the line $this->foo->function() $this is a pseudo variable that references itself so within an object of type a this code would call function() on $foo which is a reference to an object of type b. Quote Link to comment https://forums.phpfreaks.com/topic/71140-solved-question-about-classes/#findComment-357776 Share on other sites More sharing options...
noidtluom Posted September 29, 2007 Author Share Posted September 29, 2007 I am sorry. I don't really understand your post. However, my efforts based on your brief have progressed as follows: <?php class Foo { function foobar() { echo 'foobarbom.'; } } $fooInstance = new Foo; $fooInstance->foobar(); echo '<br />'; class Bar { function test() { echo $this->fooInstance->foobar(); } } $barInstance = new Bar; $barInstance->test(); ?> The error it is giving me is "Fatal error: Call to a member function on a non-object in..." I am sorry I don't know what is a member function. Googling it did not find much. Do you mind giving me a brief example? Thanks in advance. Quote Link to comment https://forums.phpfreaks.com/topic/71140-solved-question-about-classes/#findComment-357902 Share on other sites More sharing options...
rarebit Posted September 29, 2007 Share Posted September 29, 2007 Basically your creating a class variable inside another class and then calling one of it's methods, but it's a sub object remember, therefore the double '->'. The naming should give you a clue, e.g. foo class a { function test() { echo "i'm saying hi from 'a'<br>"; } } class b { $y = 99; $foo = new a(); // create a new var } $x = new b(); echo $x->y; $x->foo->test(); Quote Link to comment https://forums.phpfreaks.com/topic/71140-solved-question-about-classes/#findComment-357907 Share on other sites More sharing options...
wildteen88 Posted September 29, 2007 Share Posted September 29, 2007 You probably mean object chaining (works only with php5 or greater), an example: <?php class text { public $str; function str($str) { $this->str = $str; return $this; } function bold() { $this->str = '<b>' . $this->str . '</b>'; return $this; } function size($size) { $this->str = '<font style="font-size: ' . $size . 'px;">' . $this->str . '</font>'; return $this; } function __destruct() { echo $this->str; } } $txt = 'hello world!'; $text = new text; $text->str($txt)->bold()->size(50); ?> Quote Link to comment https://forums.phpfreaks.com/topic/71140-solved-question-about-classes/#findComment-357909 Share on other sites More sharing options...
BlueSkyIS Posted September 29, 2007 Share Posted September 29, 2007 Class Bar is invoking $this->fooinstance, but it doesn't have a fooinstance. Quote Link to comment https://forums.phpfreaks.com/topic/71140-solved-question-about-classes/#findComment-357911 Share on other sites More sharing options...
noidtluom Posted September 29, 2007 Author Share Posted September 29, 2007 Thanks guys. I'm using PHP 4 so I had to edit it a little. Here's my working test for those who are interested (others who see this topic): class Foo { function test() { echo 'Hello world!'; } } class Bar { var $x = 1; var $y; function Bar() { $this->y = new Foo; } } $foobar = new Bar; echo $foobar->x; echo $foobar->y->test(); Thanks! Topic Solved! Quote Link to comment https://forums.phpfreaks.com/topic/71140-solved-question-about-classes/#findComment-357924 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.