There are a few lines of code is not too understanding, please expert help analysis
This code is excerpted from a small fragment in the thinkphp frame.
PHP Code
/** +----------------------* Get object instance supports static method of calling Class +----------------------* @param string $class object class name * @param string $method static method name for Class +----------------------* @return Object +----------------------*/Stati C Public Function instance ($class, $method = ") {$identify = $class. $method; if (!isset (self::$_instance[$identify])) {//This line of self:: Not too clear what's going on if (class_exists ($class)) {//This is judged 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 do you explain this line? else self::$_instance[$identify] = $o; How do you explain this line? } else Halt (L (' _class_not_exist_ '). ': '. $class); Halt never seen this thing ...} return self::$_instance[$identify]; }
------Solution--------------------
Assume the name of this class: core.php
There is a static property inside the core.php
public static $_instance//Note This property is an array.
This function implements two functions:
1. Factory class: Used to create objects. If the method is empty, the class is returned.
2. Execute a method of a class. method does not empty to return the result of the method execution.
if (!isset (self::$_instance[$identify])) {//static method references are to be used with self. Self: Represents 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) as an IO operation.
self::$_instance[$identify] = Call_user_func_array (Array (& $o, $method)); This is actually a method of reflection calling a class.
self::$_instance[$identify] = $o; If the method name is empty, the object is placed in the array.
Halt (L (' _class_not_exist_ '). ': '. $class); Halt word means stop. It seems like it's time to stop compiling.
------Solution--------------------
Okayu has explained it well.
------Solution--------------------
This function implements two functions:
1. Factory class: Used to create objects. If the method is empty, the class is returned.
2. Execute a method of a class. method does not empty to return the result of the method execution.
------------------------
Read the code to look at the big picture.
It's a good explanation. Also followed the study.
From large into small.
------Solution--------------------
This class is a registered Class! Can not say is the Workshop class! Have limitations
1) If the class is not initialized, it is initialized, there is an array of class attributes, and if the class is initialized, the initialized class is returned. (The Workshop class should return a new instance each time;
2) If there is a method, but returns the result of the calling method. (Unable to pass the parameter, call any method, not the static method described)
Call_user_func_array (Array (& $o, $method)) Here & don't know if it's necessary.
------Solution--------------------
& $o seems to be $o in PHP.
and C's pointers are similar but different!
------Solution--------------------
discuss
This class is a registered Class! Can not say is the Workshop class! Have limitations
1) If the class is not initialized, it is initialized, there is an array of class attributes, and if the class is initialized, the initialized class is returned. (The Workshop class should return a new instance each time;
2) If there is a method, but returns the result of the calling method. (Unable to pass the parameter, call any method, not the static method described)
Call_user_func_array (Array (& $o, $method)) Here & do not know ...