I’m creating a website with structure like this:
class main { }
class mysql extends main { }
class user extends main { }
class etc extends main { }
The idea is for these classes to use functions from each other. This doesn’t work. How can I call a function from mysql in user?
EDIT:
All errors are similar to this one:
Fatal error: Call to undefined method user::function_in_mysql() in C:\foo.php on line 8
Martti Laine
Having a class extend another makes the methods of the other (parent) available to it. So
userextendingmainonly makes the methods ofmainavailable to it. If other classes extendmainit doesn’t allow all of them to call each others methods. You could haveuserextendmysqlandmysql‘s methods would then be available touserthough I don’t believe that fundamentally this is what you’re looking for.I think you’re looking for something along the lines of dependency injection and not class inheritance.
For example if you wanted your user class to have access to your mysql class you pass it an instance of it in it’s constructor.
Here’s some good reading on dependency injection.
The Symphony link in particular is a pretty nice read on the overview of dependency injection and how to setup a dependency container.