Do not runkit add new methods to classes dynamically in PHP5.2

Source: Internet
Author: User
Add new methods to class dynamically without Runkit in PHP5.2

The objects in JS can be dynamically added to the method. You can add a method to a class by using Runkit in PHP5.2. And to php5.3, added the closure syntax. You can add methods directly and dynamically.

Because Runkit is in the pecl, it is generally not available directly. So, in the PHP5.2, there is a limit. So, people say that PHP5.2 is still not a dynamic structure language.

The object in JS can add methods dynamically, which is always attractive to people in simplifying code, reducing inheritance and extension.

Is it really not possible to add it dynamically? In fact, it can be, just can't people find the way to achieve.

Now let's explain how to add it dynamically:

 
  lambda[$name] = $func;   }     Add the calling interface  function __call ($name, $args) {  if (array_key_exists ($name, $this->lambda)) {  $func = for dynamically added methods $this->lambda[$name];     Return $func ($args);   }  } }//Create object $OBJACST = new Activestruct (); Add a new method $objacst->addfunction (' Newfunc ', ' $vars = Func_get_args (); return ($vars [0][0]. $vars [0][1]); Test the new Method $testresult= $objAcst->newfunc (' Hello, ', ' php! '); Echo ($testResult);? >

?

It is important to note that in this way, dynamically added methods, we cannot explicitly declare the number of variables. Inside the dynamically added method. To get the arguments passed in, you can only pass:

$vars = Func_get_args ();

Regardless of the number of variables, the passed in parameters are in $vars[0]

So, to understand how many parameters are passed in, you need to use COUNT ($vars [0]);

The use of parameters is:

$vars [0][0], $vars [0][1], and so on.

?

Another disadvantage is that, because it is implemented with Create_function, the code is written as a string.

The second is the use of the __call function, which affects efficiency.

?

Nonetheless, it is possible for you to be able to add dynamic methods to the virtual hosting space. So, you can use it when you need it.

?

Of course, if you have better ways to improve, please also tell me!

?

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.