Home>Article>Backend Development> Summary of functions and classes that are easily overlooked in PHP
1. PHP functionJudge whether the function exists
When we created acustom functionand learned about thevariable functionUsage: In order to ensure that the function called by the program exists, function_exists is often used to determine whether the function exists. The same method_exists can be used to detect whether a class method exists.
function func() { } if (function_exists('func')){ echo 'exists'; }
Whether the class is defined can use class_exists
class MyClass{ } // 使用前检查类是否存在 if (class_exists('MyClass')) { $myclass = new MyClass(); }
There are many such checking methods in PHP, such as whether the file exists file_exists, etc.
$filename = 'test.txt'; if (!file_exists($filename)) { echo $filename . ' not exists.'; }
2. PHP function The variable function
The so-called variable function refers to calling a function through the value of a variable. Because the value of a variable is variable, different functions can be called by changing the value of a variable. It is often used in callback functions, function lists, or to call different functions based on dynamic parameters. The method of calling a variable function is to add parentheses to the variable name.
function name() { echo 'jobs'; } $func = 'name'; $func(); //调用可变函数
Variable functions can also be used to call methods on objects
class book { function getName() { return 'bookname'; } } $func = 'getName'; $book = new book(); $book->$func();
Static methods can also be called dynamically through variables
$func = 'getSpeed'; $className = 'Car'; echo $className::$func(); //动态调用静态方法
In static methods, $this pseudo Variables are not allowed. You can use self, parent, static to call static methods and properties internally.
class Car { private static $speed = 10; 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 attributes of two instances of the same class are equal, you can use thecomparison operator== makes a judgment. When you need to judge whether two variables are references to the same object, you can use the congruence operator === to judge.
class Car { } $a = new Car(); $b = new Car(); if ($a == $b) echo '=='; //true if ($a === $b) echo '==='; //false
Object copying, in some special cases, you can copy an object through the keyword clone. At this time, the clone method will be called, and the value of the attribute is set through thisMagic 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, you can serialize the object into a string through the serialize method, which is used to store or transfer data, and then deserialize the string into an object through unserialize when needed for use.
class Car { public $name = 'car'; } $a = new Car(); $str = serialize($a); //对象序列化成字符串 echo $str.'
'; $b = unserialize($str); //反序列化为对象 var_dump($b);
The above is the detailed content of Summary of functions and classes that are easily overlooked in PHP. For more information, please follow other related articles on the PHP Chinese website!