I am writing an application in PHP, and I need to declare external classes, while writing a flexible connection mechanism. I stopped at the overloading of properties and methods + inheritance, but I got so confused that now I don’t even know if this is possible?
I advise you to see how such things are implemented in modern frameworks (and it would be better to take one of these frameworks as a framework for your PHP application). Your implementation can not be called either flexible or convenient (or working). From what I see, you are trying to implement some kind of build application with dependency injection. Therefore, I also advise you to google Dependency Injection and how to build an application using the dependency injection container.
Also I recommend to understand the basic theory of OOP. Why did you decide that Router is Bootstrap heir? What is the area of responsibility of each of your classes? Why should Router ', which in theory should deal with the routing of incoming requests in your application, also be a collector of your application (and if you inherited Router from Bootstrap , then you have it as Bootstrap ' om)? If you want the Router have access to the behavior that you previously described in Bootstrap , and you do not want to copy pieces of code from one class to another, then inheritance is just one of the ways to reuse code, often often used by developers absolutely out of place (as in your case). If you need access to protected Bootstrap data from Router , this is also no reason to build an inheritance hierarchy between these guys. Are you trying to break encapsulation in this way? Wouldn't it be better to describe the normal public interface of Bootstrap , using which class clients would be able to interrogate it normally and get the information they need to work. If you only need Config from Bootstrap , why not pass it directly to the Router constructor?
At the same time, do not forget that, inheriting Router from Bootstrap , you are firmly tied to a specific implementation of Bootstrap , i.e. Changes to the picker class can disrupt your router.
See the last two lines in the Router class: they will cause an error that the overloaded $ config property was not found in the $ object [] array of the Bootstrap class
An error with you (judging by the code that you showed) will not come out because of this. The application will take you into an infinite loop: in the Bootstrap::init() method you create a new instance of Router , which within its constructor calls the parent init() method, inside which a new Router is created, which within its constructor calls the parent init() method init() inside which a new Router is created, which within its constructor calls the parent init() method, inside which a new Router is created ... You understand that if Router inherited from Bootstrap , then any particular $router will not be the heir of any particular exe plyara $bootstrap ? Those. when you call $bootstrap->init() , then $bootstrap will not be the "dad" of the $router instance that will be created inside its method through the new operator. $bootstrap not a prototype for you to create $router .
Your code will work if you rewrite Bootstrap::init() :
public function init(Router $router = null) { //... if (!$router) { $router = new Router($_SERVER['REQUEST_URI']); } $this->router = $router; //... }
And the init call from the Router : parent::init($this) constructor.
Or if the Router constructor is rewritten so that it Bootstrap ; or Config , if you only need it from the collector; or immediately $languages and $routes .
Nakostilyt and start the whole thing can be, but just do not need to, because for good you need to rewrite everything completely.
Want to make an application in PHP? Take literally any framework and cut it on it, reading manuals, and studying the hardware.
Is it possible to simply take the properties of the call without calling the classes themselves (or rather, their constructors)?
You can take the static properties of the class, to access them you do not need to create instances of the class. Your properties are not declared as static, so it is impossible to get them specifically without creating an instance of the class.
In general, it seems that when you wrote this code, you thought with static classes, not classes and their specific instances. And that's bad.
parent::init(). So far this is the only way to get properties from the parent class, which I know. - Mistress Denna