将对象作为变量存储在PHP类中

将对象作为变量存储在PHP类中,php,oop,Php,Oop,假设您有几个任意类,如下所示: class Foo { $foovar; public function __construct() { $foovar = "Foo"; } public function getFoo() { return($foovar); } } class Bar { $F; public function __construct() {

假设您有几个任意类,如下所示:

class Foo
{
    $foovar;

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

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

class Bar
{
    $F;

    public function __construct()
    {
        $F = new Foo();
    }
}
我的问题是,有没有可能写出下面这样的东西

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

正如我在上一个问题中所说的,我来自强大的Java背景,知道可以将变量链接在一起
System.out.println()
来调用其他对象方法。我想知道这在PHP中是否可能,这是最有可能的。

这是最有可能的。

如果
$B
中的
$F
是公共的,或者您在Bar中有一个
\u get($var)
方法愿意返回
$F
,那么
$B->F->getFoo()
工作正常。

如果
$B
中的
$F
是公共的,或者您在Bar中有一个
\uu get($var)
方法愿意返回
$F
,那么
$B->F->getFoo()
工作正常。

是的,您完全正确,请参见此示例

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();
其他一些评论

return($foovar);
  • 这里不需要帕伦夫妇
  • 您必须使用
    $this->foovar
    来处理类成员(除非是静态的,在这种情况下它是
    self::$foovar
类成员还必须指定其访问类型,例如
public$foovar;


我无法理解您为什么要从Java迁移到PHP,但祝您好运:)

是的,您完全正确,请参见此示例

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();
其他一些评论

return($foovar);
  • 这里不需要帕伦夫妇
  • 您必须使用
    $this->foovar
    来处理类成员(除非是静态的,在这种情况下它是
    self::$foovar
类成员还必须指定其访问类型,例如
public$foovar;


我无法理解您为什么要从Java迁移到PHP,但祝您好运:)

这不仅是可能的,而且非常普遍。有一次,你会看到它是与一起使用的。

这不仅是可能的,而且是非常常见的。有一次您会看到它与一起使用。

是的,但有一些附加语法。我希望使用getter方法来更好地说明问题,但是如果在类中公开变量,就不需要它们

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

是的,但有一些附加语法。我希望使用getter方法来更好地说明问题,但是如果在类中公开变量,就不需要它们

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

确保这是可能的 它被称为链接方法

// 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();
当然有可能 它被称为链接方法

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

您的示例存储的是一个对象(类实例),而不是一个类。您的示例存储的是一个对象(类实例),而不是一个类。移动是因为Java很棒,但对于很多Web开发来说,我使用PHP非常适合。移动是因为Java很棒,但是对于很多Web开发来说,我所做的PHP非常符合要求。为什么一开始就将$foovar设置为NULL?因为我将所有变量初始化为默认值;这不是必需的,但我知道为什么您在开始时将$foovar设置为NULL?因为我将所有变量初始化为默认值;没必要,但我有