S
S
sos1mple2017-07-24 22:47:59
PHP
sos1mple, 2017-07-24 22:47:59

How to implement class inheritance in PHP like in PHALCON?

Good day. Please help find a solution.
There is a class:

class Module {
public function share($moduleName, $func) {
$this->$moduleName = $func();
}
}

Next comes the following code:
$module = new Module();
$module->share('router', function(){
return new Router();
});
$module->share('acl', function(){
return new ACL();
});

The question was how to inherit an instance of the Module class with all variables

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Viktor, 2017-07-25
@sos1mple

Sounds like a misnomer for "inherit".
You create an instance of a class, store some values ​​in the resulting object, and you want... what?
So that all instances of this class receive these properties? Then you need to put the code that calls the method share()into the class constructor.
Just make another instance of the class that has the same properties? I have no idea why you need this, but you can do it. If you want a new class that will have these properties, inherit from the current class, and write the "sharing" code in the constructor of the new child class. If all these options are past - specify what exactly you need to get as a result. $newModule = clone $module;

P
Pavel Kornilov, 2017-07-24
@KorniloFF

class Parent {...}

class Child extends Parent {...}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question