What is the role of magic methods in PHP?

Source: Internet
Author: User
The magic method is also used quite a lot. Recently I was wondering why PHP should add the magic method to better support object-oriented methods? Please give me some advice when you pass by to solve your questions! The magic method is also used quite a lot. Recently I was wondering why PHP should add the magic method to better support object-oriented methods?

Please give me some advice when you pass by to solve your questions!

Reply content:

The magic method is also used quite a lot. Recently I was wondering why PHP should add the magic method to better support object-oriented methods?

Please give me some advice when you pass by to solve your questions!

Why add magic methods? Why is magic actually not the main problem?
For example, if toString is used, java can directly overwrite toString, and js can also define toString. Why is php called _ toString?
Constructor: Why is python _ init __and php _ construct?
Then, for php _ construct, it is written in the manual.

To implement backward compatibility, If PHP 5 cannot find the _ construct () function in the class and does not inherit one from the parent class, it will try to find the old constructor, that is, a function with the same name as a class. Therefore, the only situation that causes compatibility problems is that a method named _ construct () in the class is used for other purposes.

That is to say, php and java support constructor functions of the same name before. Why is it necessary? That's your confusion. My understanding is just a naming convention, you said that these functions will be automatically called in special cases, which is called magic methods. In theory, they are true, but they are also true in java. Why didn't java come up with this term?

But to be honest, php has to sum up the term "Magic method", rather than mixing it with other definitions like other languages. For example, js toString can be used to test some questions, it is easy to ignore. If php defines a magic method, you must read it all during review .. It is convenient for candidates and candidates.

In fact, to better support object-oriented, the following is an example...


  Sum2 ($ args [0], $ args [1]); break; case 3: // sum: three return $ this-> sum3 ($ args [0], $ args [1], $ args [2]); break ;}}/ *** use the magic method _ callStatic to implement pseudo-overload... * @ Return [type] [description] */public static function _ callStatic ($ name, $ args) {if ($ name = "area ") {switch (count ($ args) {case 1: // return self: areaCircle ($ args [0]); break; case 2: // calculate the rectangular area return self: areaRectangle ($ args [0], $ args [1]); break ;}}} /*** calculate the area of the circle * @ param [type] $ r [description] * @ return [type] [description] */public static function areaCircle ($ r) {return pi () * $ r ;} /*** calculate the area of the rectangle * @ param [type] $ length [description] * @ param [type] $ width [description] * @ return [type] [description] * /public static function areaRectangle ($ length, $ width) {return $ length * $ width;}/*** sum: two numbers * @ param [type] $ num1 [description] * @ param [type] $ num2 [description] * @ return [type] [description] */public function sum2 ($ num1, $ num2) {return $ num1 + $ num2;}/*** sum: three numbers * @ param [type] $ length [description] * @ param [type] $ width [description] * @ return [type] [description] */public function sum3 ($ num1, $ num2, $ num3) {return $ num1 + $ num2 + $ num3 ;}// Test start echo Tools: area (2 )."
"; Echo Tools: area (2, 4 )."
"; $ Tools = new Tools (); echo $ tools-> sum (2, 3 )."
"; Echo $ tools-> sum (2, 3, 4 )."
";

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.