0

Assume you have several arbitrary classes like below:

class Foo
{
    $foovar;

    public function __construct()
    {
        $foovar = "Foo";
    }

    public function getFoo()
    {
        return($foovar);
    }
}

class Bar
{
    $F;

    public function __construct()
    {
        $F = new Foo();
    }
}

My question is is it possible to write something like the following

$B = new Bar();
echo($B->F->getFoo());

Like I said in my previous question, I come from a strong Java background know you can link variables together System.out.println() to call other objects methods. I'd like to know if this is possible in PHP

1
  • Your example is storing an object (a class instance), not a class. Commented Jan 27, 2010 at 20:47

6 Answers 6

3

Yes you're absolutely correct, please see this example.

class Foo {
 public function hello() {
  return 'hello world';
 }
}

class Bar {
 public $driver = NULL;

 public function __construct() {
  $this->driver = new Foo;
 }
}

$test = new Bar;
echo $test->driver->hello();

Some other comments

return($foovar);
  • The parens aren't needed here.
  • You must use $this->foovar to work with class members (unless static, in which case it's self::$foovar

Class members must also have their access type specifed, for example public $foovar;.

Why you're moving from Java to PHP is beyond me, but good luck :)

Sign up to request clarification or add additional context in comments.

1 Comment

Moving because Java is great, but for a lot of the Web development I'm doing PHP fits the bill perfectly.
1

It most certainly is possible.

Comments

1

Not only is it possible but it is very common. One time you will see it used is with delegation.

Comments

0

If the $F in $B is public or you have a __get($var) method in Bar that is willing to return $F then $B->F->getFoo() works fine.

Comments

0

Yes, but with some additional syntax. I used the getter methods to hopefully illustrate things a little better, but they aren't needed if you make your variables in the class public.

<?php
class Foo
{
    private $foovar;

    public function __construct()
    {
        $this->foovar = "Foo";
    }

    public function getFooVar()
    {
        return $this->foovar;
    }
}

class Bar
{
    private $fooclass;

    public function __construct()
    {
        $this->fooclass = new Foo();
    }

    public function getFoo()
    {
        return $this->fooclass;
    }
}


$bar = new Bar();
print $bar->getFoo()->getFooVar();
?>

Comments

0

Sure it is possible Its called chaining methods

// first clean your code 

class Foo
{
    public $foovar = NULL; 

    public function __construct()
    {
        $this->foovar = "Foo";
        return $this;
    }

    public function getFoo()
    {
        return $this->foovar;
    }
}

class Bar
{
    public $F = NULL;

    public function __construct()
    {
        $this->F = new Foo();
        return $this;
    }
}

$B  = new Bar(); // create the object. The constructor returns the whole object
// so we have full access to the methods
// firs call the object $B which return in the constructor $this so we call now 
// method F now the object F returns also in the constructor the whole object $this
// we now can access the method getFoo() which will print Foo
echo $B->F->getFoo();

2 Comments

Why are you setting $foovar to NULL at the beginning?
Because i init all vars to a default value; Its not neccesary but i do

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.