How to Extend a Class Dynamically

Is it possible to extend a class dynamically?

I don't think it's possible to dynamically extend a class (however if I'm wrong I'd love to see how it's done). Have you thought about using the Composite pattern (http://en.wikipedia.org/wiki/Composite_pattern, http://devzone.zend.com/article/7)? You could dynamically composite another class (even multiple classes - this is often used as a work around to multiple inheritance) to 'inject' the methods/properties of your parent class into the child class.

PHP Class dynamically extending in runtime

Yes. It is possible. One way to do it would be using anonymous classes or simply overwriting the class itself(in your case $A) but that implies a little more logic and it's not as clean, so I won't get into it.

NOTE: Support for anonymous classes was added in PHP 7.

Using your example above we can compose the following code(I changed the visibility of the property in order to be able to use it in the extended class. I'd suggest you add a getter rather than changing the visibility).

class BasicClass {

public $variable;

public function BasicFunction() {
// do something
$this->variable = 10;
}

}

class ExtendedClass extends BasicClass {

public function ExtendedFunction() {
// do something else with basic class variable
return $this->variable / 2;
}

}

$A = new BasicClass();

if (TRUE) {

// A should be of class ExtendedClass
$A = new class extends ExtendedClass {

};

$A->ExtendedFunction();
}

Do note that this will overwrite $A. You'll still have all the available methods in it since inheritance is not lost by doing this.


Obviously whichever approach you take won't be the cleanest way you can do this.

My answer stands, but if you were to edit your question and provide more details on what it is you want to actually achieve by doing this perhaps a different approach is more suitable.


You can also achieve some magic using eval and possibly Reflection, but they're so magically magic I refuse to write the answer since it promotes such bad practices.

How to dynamically extend class with method in TypeScript?

Most often, you need to do something like:

interface UsersBlocksMyOrders {
myFunc(): any;
}

Otherwise the compiler doesn't know about it.


It even works with existing classes. For example:

interface String {
logit(): void;
}

String.prototype.logit = function () {
console.log(this);
}

let a = "string";
a.logit();

(code in playground)


Because you want to change something in a different module, which is called Module Augmentation, you need to do something like:

Import { UsersBlocksMyOrders } from "../pages/users/blocks/myorders";

declare module "../pages/users/blocks/myorders" {
interface UsersBlocksMyOrders {
logit(): void;
}
}

UsersBlocksMyOrders.prototype.logit = function () { console.log(this); }

Whenever possible (which it seems to be for you), edit the source code directly. Doing it like this should only be done on an as-needed basis.

How can I create a class dynamically that extends another dynamic class using reflection?

You can’t do that with reflection. You can create interface implementations dynamically using java.lang.reflect.Proxy but that’s it.

If you want more you have to use third-party libraries. But these libraries usually work on a lower level, e.g. byte code and require some experience. And they cannot be used in restricted environments.



Related Topics



Leave a reply



Submit