In PHP5.2, do not use Runkit to dynamically add new methods to the class.

Source: Internet
Author: User
In PHP5.2, you can dynamically add methods to objects in js without using Runkit to dynamically add new methods to classes. in PHP5.2, use Runkit to add methods to the class. in php5.3, the closure syntax is added. you can directly add methods dynamically. runkit is in PECL and cannot be used directly. therefore, in PHP5.2, it is restricted. therefore, PHP5.2 is still not a dynamic structure language. in PHP5.2, Runkit is not used to dynamically add new methods to the class.

Objects in js can be dynamically added. in PHP5.2, Runkit can be used to add methods to classes. in php5.3, the closure syntax is added. methods can be directly added dynamically.

Runkit is in PECL and cannot be used directly. Therefore, it is restricted in PHP5.2. Therefore, PHP5.2 is still not a dynamic structure language.

Objects in js can be dynamically added, which is attractive to people by streamlining code, reducing inheritance and extension.

Is it true that dynamic addition is not allowed? in fact, it is okay, but people cannot find implementation methods.

Now we will explain how to dynamically add:

 Lambda [$ name] = $ func;} // add the call Interface function _ call ($ name, $ args) {if (array_key_exists ($ name, $ this-> lambda) {$ func = $ this-> lambda [$ name]; return $ func ($ args );}}} // create an 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);?>

?

Note that using this method, we cannot explicitly declare the number of variables. in the dynamic addition method. to obtain the input parameters, you can only use:

$ Vars = func_get_args ();

No matter how many variables, the input parameters are in $ vars [0 ].

Therefore, count ($ vars [0]) is required to know how many parameters are passed in.

The usage of parameters is:

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

?

The other disadvantage is that the code is written as a string because it is implemented using create_function.

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

?

However, you can dynamically add methods to the virtual host space! Therefore, it can still be used when necessary.

?

Of course, if you have a better improvement solution, please let me know!

?

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.