How to add methods dynamically

python add method to imported class
python methodtype
python add method to class
python add method to class definition
python call class method dynamically
python add method to built-in class
python class __add__ method
python add decorator dynamically

I'm trying to add methods dynamically from external files. Right now I have __call method in my class so when i call the method I want, __call includes it for me; the problem is I want to call loaded function by using my class, and I don't want loaded function outside of the class;

Class myClass
{
    function__call($name, $args)
    {
        require_once($name.".php");
    }
}

echoA.php:

function echoA()
{
    echo("A");
}

then i want to use it like:

$myClass = new myClass();
$myClass->echoA();

Any advice will be appreciated.

You cannot dynamically add methods to a class at runtime, period.* PHP simply isn't a very duck-punchable language.

* Without ugly hacks.

Adding methods dynamically in Python, Given the dynamic nature of Python you can do many things in runtime, like add methods dynamically to an object or class. This is particularly  To add the method to the dynamic class, we will use CodeMemberMethod class, which provides different properties, which can be used to configure the definition of our methods. We will use this class to create a dynamic method, which will take two input parameters, perform their sum and return the value.

Is this what you need?

$methodOne = function ()
{
    echo "I am  doing one.".PHP_EOL;
};

$methodTwo = function ()
{
    echo "I am doing two.".PHP_EOL;
};

class Composite
{
    function addMethod($name, $method)
    {
        $this->{$name} = $method;
    }

    public function __call($name, $arguments)
    {
        return call_user_func($this->{$name}, $arguments);
    }
}


$one = new Composite();
$one -> addMethod("method1", $methodOne);
$one -> method1();
$one -> addMethod("method2", $methodTwo);
$one -> method2();

Dynamically Adding a Method to Classes or Class Instances in , In Python you sometimes want to dynamically add methods to classes or class instances (objects). In Python code, the most obvious way to  Adding methods dynamically in Python. 06 Feb 2011. Given the dynamic nature of Python you can do many things in runtime, like add methods dynamically to an object or class. This is particularly useful when writing unit tests. Here is the simplest way, adding a method to and object: class Person(object): pass def play(): print "i'm playing!"

You can dynamically add attributes and methods providing it is done through the constructor in the same way you can pass a function as argument of another function.

class Example {
    function __construct($f)
    {
       $this->action=$f;
    }
}

function fun() {
   echo "hello\n";
}

$ex1 = new class('fun');

You can not call directlry $ex1->action(), it must be assigned to a variable and then you can call this variable like a function.

How to add methods dynamically, You cannot dynamically add methods to a class at runtime, period.* PHP simply isn't a very duck-punchable language. * Without ugly hacks. In Python you sometimes want to dynamically add methods to classes or class instances (objects). In Python code, the most obvious way to accomplish this might be something like the following but it has one caveat,

if i read the manual right, the __call get called insted of the function, if the function dosn't exist so you probely need to call it after you created it

Class myClass
{
    function __call($name, $args)
    {
        require_once($name.".php");
        $this->$name($args);
    }
}

Adding Dynamically Named Methods to Python Classes, I recently wanted to dynamically add methods to a Python class based on a list… To define and execute a dynamic method Declare a delegate type to execute the method. Consider using a generic delegate to minimize the number of delegate types you need to declare. The following code declares two delegate types that could be used for the SquareIt method, and one of them is generic.

You can create an attribute in your class : methods=[] and use create_function for create lambda function. Stock it in the methods attribute, at index of the name of method you want. use :

function __call($method, $arguments)
{
   if(method_exists($this, $method))
      $this->$method($arguments);
   else
      $this->methods[$method]($arguments);
}

to find and call good method.

Is it possible to dynamically add methods to an object, or to build a , Add methods to an object dynamically during instantiation (from the constructor). "Catch attempts to access a nonexistent method" by overloading some method  I'm trying to add methods dynamically from external files. Right now I have __call method in my class so when i call the method I want, __call includes it for me; the problem is I want to call loaded function by using my class, and I don't want loaded function outside of the class;

Adding methods to Python classes « BRG blog, The normal way to add functionality (methods) to a class in Python is to define functions in the class body. There are many other ways to  Documenting a dynamic method. Just because a method is dynamic doesn’t mean it can’t be properly documented. Besides code comments around the definition of the method, a dynamically added methods should have its . __name__. and . __doc__. attributes set. The name attribute is particularly important for readable stack traces. Here’s the updated . add_description_fn. function:

Dynamic class definition in Python, Methods — functions available to an instance of that class. The classic OOP example usually involves different types of animal, or food. Here, I've  This method does not change the value of this DateTime object. Instead, it returns a new DateTime object whose value is the result of this operation. This calculates the resulting month and year, taking into account leap years and the number of days in a month, then adjusts the day part of the resulting DateTime object.

Add Methods Dynamically to Classes with ExpandoMetaClass, We can add new behaviour, like methods, to classes in Groovy dynamically. So this means a method is not added to the class definition in the source code, but  The following example demonstrates several properties and methods of the List<T> generic class, including the Add method. The parameterless constructor is used to create a list of strings with a capacity of 0. The Capacity property is displayed, and then the Add method is used to add several items.

Comments
  • From the php manual: "...All functions and classes in PHP have the global scope...". So at least: "...and I don't want loaded function outside of the class;..." will not be possible.
  • This sounds like a bad idea...
  • @Yoshi i think i didn't explained well, when i include the file that contains the function, it can be called anywhere, like: echoA(); instead of $myClass->echoA();
  • PHP is not Ruby. Once class is defined, you can't really add methods to it.
  • Please post an essential part here
  • This don't works, you get error that there are no Example::action function.
  • You can't add methods to php classes while execution.
  • i tried this 2hours ago, we cant add method by including a file that contains function(s), that's why i asked this question ;)
  • I think this could be the valid answer, besides the require_once way to call the dynamic function does not seem the way to go. I'm creating a method on the fly conditionally using the __call magic method.
  • Note that link-only answers are discouraged, SO answers should be the end-point of a search for a solution (vs. yet another stopover of references, which tend to get stale over time). Please consider adding a stand-alone synopsis here, keeping the link as a reference.