开发者

Storing objects as variables in a PHP class

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 me开发者_如何学Cthods. I'd like to know if this is possible in PHP


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


It most certainly is possible.


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


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.


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


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜