so i have a class which contains basic stuff like
Class Test{
public function wheels(){
echo 'wheels';
}
}
and another one
Class Test2{
public function bikes(){
$test = new Test();
return 'i am a bike with '.$test;
}
}
so my question is if i have different classes that i need inside a function is it good practise to call it like $var = new ClassName();
or there is a better way to call different classes inside classes functions?
For your webapp write an autoloader that is just loading all classes from files you want to use while you need it.
You can then just write the code like you want to w/o the need to build complex inheritance between classes only because you want to make use of the functionality you’ve coded.
This has multiple benefits:
That’s just my 2 cents. If you don’t like autoloader later on, you can change that, too. But it helps to get things on the run straight-forward.
Example:
The registered autoloader function will be called each time a class is instantiated/accesses while it does not exists. In the example it’s assumed that there is one class per file which is quite common and in my opinion very nice to manage the code.
As the autoloader is a callback and it’s written in PHP you can do whatever you want in it and make it much more detailed to fit your needs.
Hope this helps.