Alright, so I’m trying to figure out how to structure my code most efficiently. I’ve recently switched it from one giant class file with all my methods, to more smaller files that are combined with one base class. This is what I want, however I cannot get it to work properly and I need some help with the structure.
Essentially I need to do this:
- There will be some functions that are just “tools” for the other classes (such as converter functions, etc.) – they need to be accessible by all the child classes.
- In some cases “child class one” will need to use functions from “child class two”.
- A child class needs to be able to set a variable that another child classes can see.
Please let me know how I can get started with these requirements; code examples would be greatly appreciated!
Perhaps some pseudo code will help – but if I’m off please let me know what would work better!
class main {
private $test;
public function __construct() {
//do some stuff
}
public function getTest() {
echo('public call: '.$this->test);
}
private function randomFunc(){
echo('hello');
}
}
class child1 extends main {
public function __construct() {
parent::$test = 'child1 here';
}
}
class child2 extends main {
public function __construct() {
echo('private call: '.parent::$test); //i want this to say "private call: child1 here"
parent::randomFunc();
}
}
$base = new main;
new child1;
new child2;
$base->getTest();
So I want the result of that to be:
private call: child1 here
hello
public call: child1 here
So far what I’ve tried doesn’t work… Please help! Thank you.
First point:
For a Helper class you can choose to make your methods static, so you don’t need an instance:
That is now accessible from everywhere like this:
The next points need some further explanation, which I added as comments to your pseudo code:
I am sure this doesn’t quite answer everything, but I tried my best.
Maybe you could propose your intentions a bit further