简体   繁体   English

PHP使用class属性进入另一个类并再次使用该方法

[英]PHP Using class property into another class and use the method again

I am not aware of the technical term how to call it. 我不知道如何称呼它为技术术语。

I have a base class which has $content property holding entire page array elements. 我有一个具有$content属性的base类,该属性包含整个页面数组元素。 For some constraint, I cannot extends the base class 由于某些限制,我无法扩展base

Base Class 基类

/**
 * This is the main class
 *
 * Class base
 */
class base {

    /**
     * Holding entire page data in nested array. Such as nav item, posts, metadata etc. with the respective key => []
     * Example:
     * $this->content = [
     *  'head' => [ // some body elements such as meta tags, title etc],
     *  'body' => [
     *  ]
     * ]
     *
     * @var array
     */
    public $content = [];

    /* This is just for an example of array holding values */

    public function some_methods() {
        // various method to process the $content and return the element
        $this->content = [
            'head' => [ /* some body elements such as meta tags, title etc */ ],
                'body' => [
                    'footer' => [ /* header elements */ ],
                    'nav' => [ /* nav items */ ],
                    'article' => [
                        'text' => [ /* text and title */],
                        'metadata' => [ /* metadata */]
                    ],
                    'footer' => [ /* footer elements */ ]
                ]
            ];
    }

    public function load_navs( $navs ) {
        $one = new one();

        $one->hello($navs);
        // OR
        // some methods has loops in the `base` class
        foreach ( $navs as $key => $value ) {
            // whatever..
            $one->hello($key, $value);
        }

    }

    public function load_footer( $footer ) {
        $two = new two();
        $two->widget($footer);
    }
}

What I want to do is to make some more classes to customize some logic of the base class methods . 我要做的是使更多的类自定义basemethods某些逻辑。 For that to get a value I need to use $content of the base class into the another class. 为此,我需要将base类的$content用作另一个类。 They the methods from the newly created classes I will use in the base class methods 它们是我将在basemethods使用的新创建类的methods

One

/**
 * Class to customize or override base class logic
 * Class one
 */
class one {
    // to do some process I need to access $content from the `base` class. For example

    public function hello($navs){
        // here something to check from `nav`
        // this method I will use in base class
        if ($this->content['nav']['item']['selected']){
            // do the stuff
        }
    }
}

Two

/**
 * Class to customize or override base class logic
 * Class two
 */
class two {
    // to do some process I need to access $content from the `base` class. For example

    public function hello($footer){
        // here something to check from `footer`
        // this method I will use in base class
        if ($this->content['footer']['widget']['type'] == 'foo'){
            // do the stuff
        }
    }
}

If you can't extends your class, you can use static attribute. 如果您不能扩展您的类,则可以使用静态属性。 An static attribute is an attribute link to the class and not to an instantiated object. 静态属性是指向类而不是实例化对象的属性链接。

Look at this link for details . 请查看此链接以获取详细信息 I copy past the important informations 我复制过去的重要信息

Declaring class properties or methods as static makes them accessible without needing an instantiation of the class. 将类属性或方法声明为静态使其可以访问,而无需实例化类。

So you can declare static $content in your base Class and use it like : base::$content; 因此,您可以在基类中声明static $content ,并像这样使用它: base::$content;

    class Base {
        public static $content = [];
    }

    class One {
        function foo () {
           var_dump(Base::$content);
        }
    }

    $foo = new One();

    $foo->foo();

//show : array (size=0) empty //显示:数组(size = 0)空

And by convention, your name Classe need to upperCase (Base, One, Two) 按照约定,您的名字Classe需要大写(Base,One,Two)

EDIT : No Static, No Extends. 编辑:没有静态,没有扩展。

/* if you can't extends AND you can't use static, you can instancied Base object in you One Class. / *如果您不能扩展并且不能使用静态,则可以实例化One Class中的Base对象。 Like Tobias say in this comment, if you want change the $content for both Base and One, you need pass the var via reference. 就像Tobias在此评论中说的那样,如果要同时更改Base和One的$ content,则需要通过引用传递var。 Look at this first example, the base->$content is empty before and after One work. 看第一个示例,在一个工作之前和之后,base-> $ content为空。 And look at the 2nd example : I get the base content via reference and the content change in both case. 再看第二个示例:我通过引用获得基本内容,并且两种情况下的内容都发生了变化。 My english is probably too poor so I suggest to you to read the doc for reference. 我的英语水平可能太差了,所以我建议您阅读该文档以供参考。 */ * /

class Base {
    public $content = [];


    function displayContent() {
        var_dump($this->content);
    }

}

class One {

    function displayContent() {

        $base = new Base();

        $base->content = 'toto';

        var_dump($base->content);

    }
}

$base = new Base();
$one = new One();

$one->displayContent();
$base->displayContent();

// $one->content == 'toto'; but $base->content still empty.

Now: with reference : 现在:参考:

class Base {

    public $content = [];

    function displayContent() {
        var_dump($this->content);
    }

}

class One {

    public $content;

    function displayContent() {

        var_dump($this->content);

    }
}

$base = new Base();
$one = new One();

$one->content = &$base->content;
$one->content = 'toto';

$one->displayContent();

$base->displayContent();

// NOW, $one->content == 'toto'; and $base->content = 'toto'

Please use below mentioned lines while inherits property of base class. 在继承基类的属性时,请使用以下提到的行。

class One extends Base

class Two extends Base

Than after you can use the property of base class in child class 比之后可以在子类中使用基类的属性

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM