I am not familiar with several lines of code. please help me with the analysis. what should I do?

Source: Internet
Author: User
A few lines of code are not too understandable. Please help analyze this code from a small segment of PHPcode/** + ------------------------------------------------------------ a few lines of code are not too understandable.
This code is taken from a small clip in the ThinkPHP framework.

PHP code
  /** + ---------------------- * Obtain the static method of the object instance supporting calling class + ---------------------- * @ param string $ class object class name * @ param string $ method class static method name + ----------------------* @ return object + ---------------------- */static public function instance ($ class, $ method = '') {$ identify = $ class. $ method; if (! Isset (self ::$ _ instance [$ identify]) {// The self: This row is not too explicit. if (class_exists ($ class )) {// This is to judge if the class exists? $ O = new $ class (); if (! Empty ($ method) & method_exists ($ o, $ method) self: $ _ instance [$ identify] = call_user_func_array (array (& $ o, $ method )); // How to explain this line? Else self: $ _ instance [$ identify] = $ o; // How to explain this line? } Else halt (L ('_ CLASS_NOT_EXIST _'). ':'. $ class); // halt has never seen this thing ...... } Return self: $ _ instance [$ identify];}




------ Solution --------------------
Assume that the class name is Core. php.
Core. php has a static attribute.
Public static $ _ instance // note that this attribute is an array.


This function provides two functions:
1. factory class: used to create objects. If the method is Null, a class is returned.
2. execute methods of a class. If the method is not empty, the result of method execution is returned.

If (! Isset (self: $ _ instance [$ identify]) {// use Self to reference static methods. Self: indicates the current class itself, not the current object.

If (class_exists ($ class) {// This is to determine whether this class file exists, of course, this is the encapsulated method. You can understand File_exist (filename) in the IO operation ).

Self: $ _ instance [$ identify] = call_user_func_array (array (& $ o, $ method); // This actually reflects the method that calls a class.

Self: $ _ instance [$ identify] = $ o; // if the method name is empty, put the object in the array.

Halt (L ('_ CLASS_NOT_EXIST _'). ':'. $ class); // halt indicates stop. It seems that the compilation is stopped.
------ Solution --------------------
Okayu has already explained this.
------ Solution --------------------
This function provides two functions:
1. factory class: used to create objects. If the method is Null, a class is returned.
2. execute methods of a class. If the method is not empty, the result of method execution is returned.
------------------------
Code reading should focus on the overall situation.
The explanation is really good. Also followed by learning.

From large to small.
------ Solution --------------------
This is a registration class! It cannot be a workshop! Limitations

1) If the class is not initialized, the class will be initialized and there will be an array of class properties; if the class has been initialized, the class will be returned. (The Workshop class should return a new instance each time;

2) If there is a method, but the result of calling the method is returned. (Parameters cannot be passed. any method is called instead of static methods)

Call_user_func_array (array (& $ o, $ method) here & I do not know if it is necessary.




------ Solution --------------------
& $ O in php seems to be $ o
Similar to the pointer of C, but different!
------ Solution --------------------
Discussion
This is a registration class! It cannot be a workshop! Limitations

1) If the class is not initialized, the class will be initialized and there will be an array of class properties; if the class has been initialized, the class will be returned. (The Workshop class should return a new instance each time;

2) If there is a method, but the result of calling the method is returned. (Parameters cannot be passed. any method is called instead of static methods)

Call_user_func_array (array (& $ o, $ method ......

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.