


Detailed explanation of commonly used keywords and magic methods in PHP object-oriented
This article mainly introduces the detailed explanation of keywords and magic methods commonly used in PHP object-oriented. Friends who are interested can refer to it. I hope it will be helpful to everyone.
Commonly used keywords in PHP object-oriented
final
1.final cannot modify member attributes (this keyword is not used for constants in classes)
2.final can only modify classes and methods
Function:
Classes modified with final cannot be inherited by subclasses
Methods modified with final cannot be overridden by subclasses
It is used to restrict the class from being inherited. If the method is not overwritten, use final
<?php //final修饰的类不能被继承 final class Person{ var $name; var $age; var $sex; function __construct($name,$age,$sex){ $this->name=$name; $this->age=$age; $this->sex=$sex; } function fun1(){ echo $this->name; } } //Student类继承类用final修饰的Person类,所以会报错 class Student extends Person{ } $stu=new Student("zs",20,"nan"); $stu->fun1(); ?>
static (static keyword)
1. Use static to modify Member properties and member methods cannot modify the class
2. Member properties modified with static can be shared by all objects of the same class
3. Static data is data stored in memory segment (initializing the static segment)
4. Static data is allocated to memory when the class is loaded for the first time. When the class is used in the future, it is obtained directly from the data segment
5.What is a class loaded? As long as this class is used in the program (this class name appears)
6. Static methods (static modified methods) cannot access non-static members (static members can be accessed in non-static methods)
Because non-static members must be accessed using objects, $this is used to access internal members. Static methods do not need to be called with objects, so there is no object. $this cannot represent objects. Non-static Members must also use objects
If you are sure that non-static members are not used in a method, you can declare this method as a static method
Note: Static members must use class names To access, do not create objects, do not use objects to access
Class name::static member
If you use static members in a class, you can use self to represent this class
const
1. It can only modify member properties
2. Use const
to declare constant properties in the class 3.Access method Same as static member attributes (use class name::constant outside the class and self::constant inside the class)
4. Constants must be given initial values when they are declared
<?php //定义一个类“人们” class Person{ protected $name; protected $age; protected $sex; static $country="中国"; //声明一个常量 const RUN="走"; //构造方法 function __construct($name,$age,$sex){ $this->name=$name; $this->age=$age; $this->sex=$sex; } function getCountry(){ //如果在类中使用静态成员,可以使用self代表本类 return self::$country; } function say(){ echo "我的名字:{$this->name},我的年龄:{$this->age},我的性别:{$this->sex}。<br>"; } protected function eat(){ echo "吃饭!<br>"; } function run(){ //在类的内部使用常量 self::常量 echo self::RUN."<br>"; } //声明静态的方法 static function hello(){ echo "你好<br>"; } }
Magic methods commonly used in PHP object-oriented
__call()
Function: When calling a method that does not exist in the object, a system error will appear, and then the program will exit.
When to call automatically: It will be called automatically when calling a method that does not exist in an object
Handle some non-existent error calls
This method requires two Parameters
<?php //定义一个类“人们” class Person{ protected $name; protected $age; protected $sex; static $country="中国"; //声明一个常量 const RUN="走"; //构造方法 function __construct($name,$age,$sex){ $this->name=$name; $this->age=$age; $this->sex=$sex; } function getCountry(){ //如果在类中使用静态成员,可以使用self代表本类 return self::$country; } function say(){ echo "我的名字:{$this->name},我的年龄:{$this->age},我的性别:{$this->sex}。<br>"; } protected function eat(){ echo "吃饭!<br>"; } function run(){ //在类的内部使用常量 self::常量 echo self::RUN."<br>"; } //处理一些不存在的错误调用 //就会在调用一个对象中不存在的方法时就会自动调用 function __call($methodName,$args){ //$methodName调用不存在方法的方法名 $args里面的参数 echo "你调用的方法{$methodName}(参数:"; print_r($args); echo ")不存在<br>"; } //声明静态的方法 static function hello(){ echo "你好<br>"; } } $p=new Person("张三",20,"女"); $p->test(10,20,30); $p->demo("aa","bb"); $p->say(); ?>
__toString()
Automatically called when directly outputting an object reference , the fastest way to quickly obtain a string representation
<?php //定义一个类“人们” class Person{ protected $name; protected $age; protected $sex; static $country="中国"; //声明一个常量 const RUN="走"; //构造方法 function __construct($name,$age,$sex){ $this->name=$name; $this->age=$age; $this->sex=$sex; } function say(){ echo "我的名字:{$this->name},我的年龄:{$this->age},我的性别:{$this->sex}。<br>"; } function __toString(){ return self::$country."<br>{$this->name}<br>{$this->age}<br>{$this->sex}<br>".self::RUN; } } $p=new Person("张三",21,"女"); echo $p; ?>
##__clone()
Clone objects are processed using clone()Original (original object)Copy (copied object)__clone() is when cloning an object Automatically called methodAs soon as an object is created, there must be an initialization action, which is similar to the constructor method __constuctThe $this keyword in the __clone() method represents It is a copy object, $that represents the original object<?php //定义一个类“人们” class Person{ var $name; protected $age; protected $sex; static $country="中国"; //声明一个常量 const RUN="走"; //构造方法 function __construct($name,$age,$sex){ $this->name=$name; $this->age=$age; $this->sex=$sex; } function say(){ echo "我的名字:{$this->name},我的年龄:{$this->age},我的性别:{$this->sex}。<br>"; } function __clone(){ $this->name="王五"; $this->age=18; $this->sex="男"; } function __destruct(){ echo $this->name."<br>"; } } $p=new Person("张三",21,"女"); $p->say(); //这并不能叫做克隆对象,因为在析构时只析构一次 /*$p1=$p; $p1->name="李四"; $p1->say();*/ $p1= clone $p; $p1->say(); ?>
##__autoload()Note: Other magic methods are added to the class. This is the only method that is not added to the class.
As long as a class is used in the page, as long as the class name is used, it will automatically Pass this class name to this parameter
<?php function __autoload($className){ include "./test/".$className.".class.php"; } $o=new One; $o->fun1(); $t=new Two; $t->fun2(); $h=new Three; $h->fun3(); ?>
File in test
one.class.php
<?php class One{ function fun1(){ echo "The Class One<br>"; } } ?>
two.class.php
<?php class Two{ function fun2(){ echo "The Class Two<br>"; } } ?>
three.class.php
<?php class Three{ function fun3(){ echo "The Class Three<br>"; } } ?>
Object serialization (serialization): Convert an object into a binary string (the object is stored in memory and is easy to release)
Usage time: 1. When objects are stored in databases or files for a long time
2. When objects are transferred in multiple PHP files
serialize(); The parameter is an object, and what is returned is the serialized binary string
unserialize(); The parameter is the binary string of the object, and what is returned is the newly generated object
__sleep() is a method called during serialization
Function: It can partially serialize an object Serialization
As long as this method returns an array, several member properties in the array will be serialized. If this method is not added, all members will be serialized
__wakeup()
是在反序列化时调用的方法
也是对象重新诞生的过程
<?php //定义一个类“人们” class Person{ var $name; protected $age; protected $sex; static $country="中国"; //声明一个常量 const RUN="走"; //构造方法 function __construct($name,$age,$sex){ $this->name=$name; $this->age=$age; $this->sex=$sex; } function say(){ echo "我的名字:{$this->name},我的年龄:{$this->age},我的性别:{$this->sex}。<br>"; } function __clone(){ $this->name="王五"; $this->age=18; $this->sex="男"; } //是在序列化时调用的方法,可以部分串行化对象 function __sleep(){ return array("name","age"); } //是在反序列化时调用的方法,也是对象重新诞生的过程。可以改变里面的值 function __wakeup(){ $this->name="sanzhang"; $this->age=$this->age+1; } function __destruct(){ } } ?>
read.php
<?php require "11.php"; $str=file_get_contents("mess.txt"); $p=unserialize($str); echo $p->say(); ?>
write.php
<?php require "11.php"; $p=new Person("张三",18,"男"); $str=serialize($p); file_put_contents("mess.txt",$str); ?>
以上就是本文的全部内容,希望对大家的学习有所帮助。
相关推荐:
PHP中关键字interface和implements图文详解
The above is the detailed content of Detailed explanation of commonly used keywords and magic methods in PHP object-oriented. For more information, please follow other related articles on the PHP Chinese website!

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP makes it easy to create interactive web content. 1) Dynamically generate content by embedding HTML and display it in real time based on user input or database data. 2) Process form submission and generate dynamic output to ensure that htmlspecialchars is used to prevent XSS. 3) Use MySQL to create a user registration system, and use password_hash and preprocessing statements to enhance security. Mastering these techniques will improve the efficiency of web development.

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP remains important in modern web development, especially in content management and e-commerce platforms. 1) PHP has a rich ecosystem and strong framework support, such as Laravel and Symfony. 2) Performance optimization can be achieved through OPcache and Nginx. 3) PHP8.0 introduces JIT compiler to improve performance. 4) Cloud-native applications are deployed through Docker and Kubernetes to improve flexibility and scalability.

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP and Python each have their own advantages and are suitable for different scenarios. 1.PHP is suitable for web development and provides built-in web servers and rich function libraries. 2. Python is suitable for data science and machine learning, with concise syntax and a powerful standard library. When choosing, it should be decided based on project requirements.

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 Linux new version
SublimeText3 Linux latest version

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Notepad++7.3.1
Easy-to-use and free code editor