如何从一个类到另一个类变量?

时间:2011-09-05 15:18:34

标签: php

我是PHP的新手,并且在Example_02类中有问题。

<?php

class Entity 
{ 

    private $components = array(); 

    public function add_component(Component $component) 
    { 
        if (in_array($component, $this->components) == false) 
            $this->components[] = $component; 
    } 

    public function get_component(Component $component) 
    { 
        if (in_array($component, $this->components) == true) 
            return $this->components[array_search($component, $this->components)]; 
    } 

} 

class Component 
{ 



} 

class Example_01 extends Component 
{ 

    public $example_var; 

    public function __construct() 
    { 

    } 

} 

class Example_02 extends Component 
{ 

    public function __construct() 
    { 
        // how to get $example_var from Example_01 class? 
    } 

} 

$ent = new Entity(); 
$ent->add_component(new Example_01()); 
$ent->add_component(new Example_02()); 

var_dump($ent); 

?>

4 个答案:

答案 0 :(得分:0)

您应该在$example_var基类中保留(或“注册”)Entity。这样,当基类通过Example_02添加时,它可以将其传递给add_component()类。

答案 1 :(得分:0)

您可以将Example_01对象传递给Example_02

class Example_02 extends Component 
{ 

    public function __construct($example2) 
    { 
        $example2->variable;
    } 

} 

这只适用于变量标记为public,最好在Example_01中创建一个getVariable()方法。

答案 2 :(得分:0)

在课程中,member variables (properties)在实例化课程之前不能有特定的值。

你可以:

制作变量staticpublic static $example_var;

首先实现Example_01,然后设置并获取$example_var

答案 3 :(得分:0)

通过基类相互链接的3个类的示例。希望我不是太错了。 :-s

<?php 
/**base class with getters/setters**/
Class Entity {
    private $vars = array();

    public function __set($index, $value){
        $this->vars[$index] = $value;
    }
    public function __get($index){
        return $this->vars[$index];
    }
}

/*On __construct pass the entity class
now $entity->first = this object so $entity->first->something() is the internal method
*/ 
class first {
    private $entity;

    function __construct($entity) {
        $this->entity = $entity;
    }

    public function something(){
        return 'Test string';
    }
}

/*On __construct pass the entity class
now $entity->second = this object so $entity->second->test() is the internal method
*/ 
class second {

    private $entity;

    function __construct($entity) {
        $this->entity = $entity;
    }

    public function test(){
        echo $this->entity->first->something();
    }
}


//Note the passing of $entity to all the sub classes.
$entity = new Entity;
$entity->first = new first($entity);
$entity->second = new second($entity);

//Go through second class to retrive method reslt from first class
$entity->second->test(); //result: Test string

print_r($entity);
/*
Entity Object
(
    [vars:Entity:private] => Array
        (
            [first] => first Object
                (
                    [entity:first:private] => Entity Object
 *RECURSION*
                )

            [second] => second Object
                (
                    [entity:second:private] => Entity Object
 *RECURSION*
                )

        )

)
*/
?>