1. Whether the function of PHP function exists
When we create custom functions and understand the use of mutable functions, in order to ensure that the functions called by the program are present, we often use function_exists to determine whether a function exists. The same method_exists can be used to detect whether a class's methods exist.
function func () {}if (function_exists (' func ')) {echo ' exists ';}
class defines whether the class_exists can be used
Class myclass{}//checks for the existence of an if (class_exists (' MyClass ')) {$myclass = new MyClass () before use;}
There are many such checks in PHP, such as whether the file exists file_exists, etc.
$filename = ' Test.txt ', if (!file_exists ($filename)) {echo $filename. ' Not exists. ';}
2. Variable function of PHP function
The so-called mutable function, that is, the function is called by the value of the variable, because the value of the variable is variable, so you can call different functions by changing the value of a variable. Often used in callback functions, function lists, or depending on dynamic parameters to invoke different functions. The variable function is called by the variable name in parentheses.
Function name () {echo ' Jobs ';} $func = ' name '; $func (); Calling a mutable function
Variable functions can also be used on the object's method calls
Class Book {function GetName () { return ' BookName ';}} $func = ' GetName '; $book = new book (); $book-$func ();
Static methods can also be dynamically called through variables
$func = ' getspeed '; $className = ' Car '; Echo $className:: $func (); Dynamic invocation of static methods
In a static method, $this pseudo-variable is not allowed. You can use Self,parent,static to invoke static methods and properties internally.
Class Car {private static $speed = ten; public static function GetSpeed () { return self:: $speed;} public static function SpeedUp () { return self:: $speed +=10;}} Class Bigcar extends Car {public static function start () { parent::speedup ();}} Bigcar::start (); Echo Bigcar::getspeed ();
3. Advanced features of PHP classes and objects
Object comparison, when all the properties of two instances of the same class are equal, you can use the comparison operator = = To determine whether a two variable is a reference to the same object, you can use the congruent operator = = =.
Class Car {} $a = new car (), $b = new Car (), if ($a = = $b) echo ' = = '; Trueif ($a = = = $b) echo ' = = = '; False
Object replication, in some special cases, you can copy an object by using the keyword clone, in which case the Clone method is called, and the value of the property is set by this magic method.
Class Car {public $name = ' car '; Public Function Clone () { $obj = new Car (); $obj->name = $this->name; }} $a = new car (); $a->name = ' new car '; $b = clone $a; var_dump ($b);
Object serialization, which can be serialized as a string by the Serialize method, used to store or pass data, and then deserialize the string into an object when needed by Unserialize.
Class Car {public $name = ' car ';} $a = new Car (); $str = serialize ($a); The object is serialized into a string echo $str. ' <br> '; $b = Unserialize ($STR); Deserializes to Object Var_dump ($b);