开发者

How to get variable from one class to another?

开发者 https://www.devze.com 2023-04-02 07:39 出处:网络
I am a newbie to PHP and have question which writen in Example_02 class. <?php class Entity { private $components = array();

I am a newbie to PHP and have question which writen in Example_02 class.

<?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 Exam开发者_运维问答ple_01()); 
$ent->add_component(new Example_02()); 

var_dump($ent); 

?>


An example of 3 classes interlinked together through a base class. Hope im not too wrong. :-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*
                )

        )

)
*/
?>


You should keep (or 'register') the $example_var in the Entity base class. That way, the base class can pass it on to the Example_02 class when it's added through add_component().


you could pass the Example_01 object to Example_02

class Example_02 extends Component 
{ 

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

} 

This only works with having variable marked public it is better to create a getVariable() method in Example_01.


In a class, member variables (properties) can't have have a specific value until the class in instanciated.

You can:

Make your variable static: public static $example_var;, or

First instanciate Example_01 and then set and get $example_var

0

精彩评论

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