Change the value of the protected variable for the child class

I am trying to do something that, in my opinion, will be quite simple, but since I am relatively new to OOP in PHP, I have problems.

Here is my code:

<?php class MyClass { protected $variable = 'DEFAULT'; function __construct($newVariable) { $this->variable = $newVariable; echo $this->variable; } } class OtherClass extends MyClass { function __construct() { echo $this->variable; } } $foo = new MyClass('MODIFIED'); // Output : MODIFIED $bar = new OtherClass(); // Output : DEFAULT ?> 

I searched a lot of different streams and websites, but did not find how I can pass the overridden value of the $ variable to the child class. Can someone help me with this? If there is a way.

Thanks in advance.

EDIT Explaining what actually happens in the script I had this problem: I have a class that creates a form, and in this class there are several variables that describe functions in the environment in which we are: if it is a multilingual form or no, if I want a simple or structured, such options. And some of these environment variables are changed by executing a class according to its functions.

Now, due to the needs of my page, I have a child class designed to create select fields. It inherits the main function of the main class for creating fields and such, but the child class takes a few more parameters. The fact is that I would still like the select class to inherit the environment parameters from the main class in the state in which they are.

As I type this, I understand how I approached it, maybe this is not the right way to do this, because when I look at my code, I think there may be another way to do this. In my code, I have two objects where I need it to be only one, but I really don't know how it really would be.

+6
source share
5 answers

You do not override the variable, you just set it in the parent constructor. Name it, and you have what you need (see Comment in code, only one line):

 <?php class MyClass { protected $variable = 'DEFAULT'; function __construct($newVariable) { $this->variable = $newVariable; echo $this->variable; } } class OtherClass extends MyClass { function __construct() { parent::__construct(); # <<--- do this echo $this->variable; } } $foo = new MyClass('MODIFIED'); // Output : MODIFIED $bar = new OtherClass(); // Output : MODIFIED ?> 

Even if you have a definition of two classes, a particular instance will always be of the same class. This class can inherit from the parent class, but if you override the methods (for example, you did this with the constructor), the method of the parent class will not be called because it has been overridden. However, with the parent keyword, you can access the parent method from the child.

+2
source

The constructor of MyClass overridden in OtherClass . The parent constructor implicitly automatically starts. If you want to run the parent constructor, call parent::__construct() in OtherClass::__construct .

You, of course, have a problem, since MyClass::__construct expects a parameter, while OtherClass::__construct does not. Putting it this way, how did you expect the value to change ...?

+1
source

The child class only inherits default methods and variables. It does not inherit the properties of the root class if you start a new instance, because $foo and $bar are two different objects.

You will need to do $bar = new OtherClass("MODIFIED"); and then add parent::__construct($newVariable) to the second __construct. (and corresponding parameter)

Basically, you cannot expect a new instance (which is parallel to $ foo) to β€œmagically” inherit the modified from the original $ foo. You should find a way to pass it (therefore, by calling the parent constructor).

+1
source

You need to add the parent call to the child class:

 function __construct() { parent::__construct(); echo $this->variable; } 

You can do this with any function:

 function doSomethingAWESOME() { parent::doSomethingAWESOME(); echo "I think that was awesome"; } 

And basically, the statement is this: "Do you know everything that you are going to do in the parent class before I redefine it? Yes, do it here."

Also cool:

 function doSomethingAWESOME() { echo "NEVER!!!"; } function parentSomethingAwesome() { // calls the parent class definition of some doSomethingAWESOME parent::doSomethingAWESOME(); } 
0
source

What you want to do is impossible, because: $ foo and $ bar are instances of your classes, so there are no links between them.

To use the structural analogy, your two classes are similar to building plans: a house plan and a house plan with a garage. What you are trying to do is that you are building a house (for example, a class), insert some kind of bed in it (set some variables), and when you build another house with a garage, you need the same bed . This will not happen.

0
source

Source: https://habr.com/ru/post/893686/


All Articles