Jump to content

[SOLVED] Question about Classes


noidtluom

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/71140-solved-question-about-classes/
Share on other sites

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.

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.

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();

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);
?>

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!

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.