Home > Backend Development > PHP Tutorial > Summary of functions and classes that are easily overlooked in PHP

Summary of functions and classes that are easily overlooked in PHP

伊谢尔伦
Release: 2023-03-11 16:16:01
Original
986 people have browsed it

1. PHP function Judge whether the function exists

When we created a custom function and learned about the variable function Usage: 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';
}
Copy after login

Whether the class is defined can use class_exists

class MyClass{
}
// 使用前检查类是否存在
if (class_exists('MyClass')) {
 $myclass = new MyClass();
}
Copy after login

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.';
}
Copy after login

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(); //调用可变函数
Copy after login

Variable functions can also be used to call methods on objects

class book {
 function getName() {
  return 'bookname';
 }
}
$func = 'getName';
$book = new book();
$book->$func();
Copy after login

Static methods can also be called dynamically through variables

$func = 'getSpeed';
$className = 'Car';
echo $className::$func(); //动态调用静态方法
Copy after login

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();
Copy after login

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 the comparison 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
Copy after login

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 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);
Copy after login

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.&#39;<br>&#39;;
$b = unserialize($str); //反序列化为对象
var_dump($b);
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template