


A few notes on the improved object-oriented approach to PHP Page 1/2_PHP Tutorial
先看代码:
class StrictCoordinateClass {
private $arr = array('x' => NULL, 'y' => NULL);
function __construct()
{
print "StrictCoordinateClass is being created";
print "
";
}
function __destruct()
{
print "
";
print "StrictCoordinateClass is being destroyed";
}
function __get($property)
{
if (array_key_exists($property, $this->arr)) {
return $this->arr[$property];
} else {
print "Error: Can't read a property other than x & yn";
}
}
function __set($property, $value)
{
if (array_key_exists($property, $this->arr)) {
$this->arr[$property] = $value;
} else {
print "Error: Can't write a property other than x & yn";
}
}
}
$obj = new StrictCoordinateClass();
$obj->x = 1;
print $obj->x;
print "
";
$obj->n = 2;
print "
";
print $obj->n;
?>
Output result:
StrictCoordinateClass is being created
1
Error: Can't write a property other than x & y
Error: Can't read a property other than x & y
StrictCoordinateClass is being destroyed
__construct() and __destruct() are equivalent to the constructor in Java and the destructor in C.
As for __get and __set, please see below:
Reference from: http://www.phpchina.com/html/54/26354-31906.html
.__set() __get() __isset( ) Application of the four methods of __unset()
Generally speaking, always define the attributes of a class as private, which is more in line with realistic logic. However, reading and assigning operations to attributes are very frequent, so in PHP5, two functions "__get()" and "__set()" are predefined to obtain and assign their attributes, as well as "__isset" to check the attributes. ()" and the method to delete attributes "__unset()".
In the previous section, we set and obtained methods for each attribute. PHP5 provides us with special methods for setting and obtaining values for attributes, "__set()" and "__get()" These two methods, these two methods do not exist by default, but we add them to the class manually. Like the constructor method (__construct()), it will only exist if it is added to the class. You can add it in the following way Of course, these two methods can also be added according to personal style:
//__get() method is used to obtain private properties
private function__get($property_name)
{
if(isset($ this->$property_name))
{
return($this->$property_name);
}else
{
return(NULL);
}
}
//__set() method is used to set private properties
private function__set($property_name,$value)
{
$this->$property_name=$value;
}
__get() method: This method is used to get the private member attribute value. It has one parameter. The parameter is passed in the name of the member attribute you want to get, and the obtained attribute value is returned. This method does not need to be called manually, because We can also make this method a private method, which is automatically called by the object when the private property is directly obtained. Because the private properties have been encapsulated, the value cannot be obtained directly (for example: "echo $p1->name" is wrong to obtain directly), but if you add this method to the class, use " When a statement like "echo $p1->name" directly obtains the value, the __get($property_name) method will be automatically called, and the property name will be passed to the parameter $property_name. Through the internal execution of this method, the private value we passed in will be returned. The value of the attribute. If the member properties are not encapsulated as private, the object itself will not automatically call this method.
__set() method: This method is used to set values for private member attributes. It has two parameters. The first parameter is the name of the attribute you want to set the value for, and the second parameter is the value you want to set for the attribute. , no return value. This method also does not need to be called manually. It can also be made private. It is automatically called when directly setting the private attribute value. The same private attribute has been encapsulated with
. If there is no __set() Methods are not allowed, for example: $this->name='zhangsan', this will cause an error, but if you add the __set($property_name, $value) method to the class, you can directly set the private property When assigning a value, it will be automatically called, passing the attribute such as name to $property_name, and passing the value "zhangsan" to be assigned to $value. Through the execution of this method, the purpose of assignment is achieved. If the member properties are not encapsulated as private, the object itself will not automatically call this method. In order not to pass in illegal values, you can also make a judgment in this method. The code is as follows:
classPerson
{
//The following are the member attributes of the person, which are all encapsulated private members
private $name; //The person’s name
private $sex; //Person’s gender
private $age; //Person’s age
//__get() method is used to obtain private properties
private function__get($property_name)
{
echo"When directly obtaining the private property value, this __get() method is automatically called
";
if(isset($this->$property_name))
{
return($this->$property_name);
}
else
{
return(NULL);
}
}
//__set() method Used to set private properties
private function__set($property_name,$value)
{
echo" When directly setting the value of a private property, this __set() method is automatically called to assign a value to the private property";
$this->$property_name=$value;
}
}
$p1=newPerson();
//The operation of directly assigning a value to a private property will Automatically call the __set() method to assign values
$p1->name="Zhang San";
$p1->sex="Male";
$p1->age=20;
//Get the value of the private attribute directly, the __get() method will be automatically called to return the value of the member attribute
echo "Name:".$p1->name."
";
echo"Gender:".$p1->sex."
";
echo"Age:".$p1->age."
";
?> ;

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values and handle functions that may return null values.

In PHP, use the clone keyword to create a copy of the object and customize the cloning behavior through the \_\_clone magic method. 1. Use the clone keyword to make a shallow copy, cloning the object's properties but not the object's properties. 2. The \_\_clone method can deeply copy nested objects to avoid shallow copying problems. 3. Pay attention to avoid circular references and performance problems in cloning, and optimize cloning operations to improve efficiency.

PHP is suitable for web development and content management systems, and Python is suitable for data science, machine learning and automation scripts. 1.PHP performs well in building fast and scalable websites and applications and is commonly used in CMS such as WordPress. 2. Python has performed outstandingly in the fields of data science and machine learning, with rich libraries such as NumPy and TensorFlow.


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

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

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.