tags:

views:

86

answers:

6

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  A: 

It most certainly is possible.

Aaron
A: 

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.

dnagirl
+3  A: 

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 :)

The Pixel Developer
Moving because Java is great, but for a lot of the Web development I'm doing PHP fits the bill perfectly.
Josh K
+1  A: 

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

John Conde
A: 

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();
?>
sfrench
A: 

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();
streetparade
Why are you setting $foovar to NULL at the beginning?
Josh K
Because i init all vars to a default value; Its not neccesary but i do
streetparade