0

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. For some constraint, I cannot extends the base class

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. For that to get a value I need to use $content of the base class into the another class. They the methods from the newly created classes I will use in the base class 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
        }
    }
}
2
  • First of all: Are you really not allowed to extend the Base class? If so and you just want to use the $content within the methods in one and two, you can pass it as a second argument. Please be aware if you want to modify the original $content you will have to pass it by reference. Commented Jun 9, 2017 at 9:47
  • Why you can't use extends ? And can you use static property ? Commented Jun 9, 2017 at 9:50

2 Answers 2

2

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;

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

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

    $foo = new One();

    $foo->foo();

//show : array (size=0) empty

And by convention, your name Classe need to upperCase (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. Like Tobias say in this comment, if you want change the $content for both Base and One, you need pass the var via reference. Look at this first example, the base->$content is empty before and after One work. 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'
Sign up to request clarification or add additional context in comments.

11 Comments

"For some constraint, I cannot extends the base class" The OP stated that this is not an option to use.
Yes thank for your reflection. I've add new suggestion (static attribute) but maybe he can't use static too. I've asked to him and I edit my post then he give more explains
Yeah, static might be a good way to go if the OP is allowed to change the base class.
I am still on mid level of OOP. So will it allow without instantiating the class?
Thanks.. I am waiting :)
|
0

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

1 Comment

"For some constraint, I cannot extends the base class" The OP stated that this is not an option to use.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.