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');
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.
source share