Home > Article > Backend Development > What does php oop mean?
php oop refers to object-oriented programming. In object-oriented programming (OOP), an object is a whole composed of information and a description of how to process the information. It is an abstraction of the real world.
The operating environment of this article: Windows7 system, PHP7.1 version, DELL G3 computer
What does php oop mean?
PHP Object-oriented
In object-oriented programming (English: Object-oriented programming, abbreviation: OOP), an object is a collection of information and The whole description of the processing of information is an abstraction of the real world.
In the real world, the things we face are objects, such as computers, televisions, bicycles, etc.
The main three characteristics of the object:
#The behavior of the object: what operations can be applied to the object, turning on the light and turning off the light are behaviors.
The shape of the object: how the object responds, color, size, and appearance when those methods are applied.
Representation of objects: The representation of objects is equivalent to an ID card, specifically distinguishing the differences in the same behavior and status.
For example, Animal is an abstract class. We can specify a dog and a sheep, and dogs and sheep are concrete objects. They have color attributes and can be written , you can run and other behavioral states.
Object-oriented content
Class − defines the abstract characteristics of a thing. The definition of a class includes the form of the data and the operations on the data.
Object − is an instance of a class.
Member variables − Variables defined inside the class. The value of this variable is invisible to the outside world, but can be accessed through member functions. After the class is instantiated as an object, the variable can become an attribute of the object.
Member function − Defined inside the class, it can be used to access the data of the object.
Inheritance − Inheritance is a mechanism for subclasses to automatically share data structures and methods of parent classes. This is a relationship between classes. When defining and implementing a class, you can do it on the basis of an existing class, take the content defined by the existing class as your own content, and add some new content.
Parent class − A class is inherited by other classes. This class can be called a parent class, a base class, or a super class.
Subclass − A class that inherits other classes is called a subclass or a derived class.
Polymorphism − Polymorphism means that the same function or method can act on multiple types of objects and obtain different results. Different objects can produce different results when receiving the same message. This phenomenon is called polymorphism.
Overloading − Simply put, it is a situation where functions or methods have the same name but different parameter lists. Such functions or methods with the same name and different parameters are called overloaded functions or methods. .
Abstraction − Abstraction refers to abstracting objects with consistent data structures (attributes) and behaviors (operations) into classes. A class is an abstraction that reflects important properties related to an application while ignoring other irrelevant content. The division of any class is subjective, but must be related to the specific application.
Encapsulation − Encapsulation refers to binding the properties and behavior of an object that exists in the real world and placing it in a logical unit.
Constructor − Mainly used to initialize the object when creating the object, that is, assign initial values to the object member variables. It is always used together with the new operator in the statement to create the object.
Destructor − Destructor (destructor) Contrary to the constructor, when the object ends its life cycle (for example, the function in which the object is located has been called), the system automatically executes the destructor. Destructors are often used to do "clean-up" work (for example, when creating an object, use new to open up a memory space, which should be released with delete in the destructor before exiting).
In the figure below, we have created three objects through the Car class: Mercedes, Bmw, and Audi.
$mercedes = new Car (); $bmw = new Car (); $audi = new Car ();
PHP class definition
The usual syntax format of PHP definition class is as follows:
<?php class phpClass { var $var1; var $var2 = "constant string"; function myfunc ($arg1, $arg2) { [..] } [..] } ?>
The analysis is as follows:
A class is defined using the class keyword followed by the class name.
Variables and methods can be defined within a pair of braces ({}) after the class name.
Variables of the class are declared using var, and variables can also be initialized.
Function definition is similar to the definition of PHP function, but the function can only be accessed through the class and its instantiated objects.
Instance
<?php class Site { /* 成员变量 */ var $url; var $title; /* 成员函数 */ function setUrl($par){ $this->url = $par; } function getUrl(){ echo $this->url . PHP_EOL; } function setTitle($par){ $this->title = $par; } function getTitle(){ echo $this->title . PHP_EOL; } } ?>
Variable $this represents the object of itself.
PHP_EOL is the newline character.
Create objects in PHP
After the class is created, we can use the new operator to instantiate objects of this class:
$runoob = new Site; $taobao = new Site; $google = new Site;
We have the above code Three objects have been created. Each of the three objects is independent. Next, let's take a look at how to access member methods and member variables.
Call member methods
After instantiating an object, we can use the object to call member methods. The member methods of the object can only operate the member variables of the object:
// 调用成员函数,设置标题和URL $runoob->setTitle( "菜鸟教程" ); $taobao->setTitle( "淘宝" ); $google->setTitle( "Google 搜索" ); $runoob->setUrl( 'www.runoob.com' ); $taobao->setUrl( 'www.taobao.com' ); $google->setUrl( 'www.google.com' ); // 调用成员函数,获取标题和URL $runoob->getTitle(); $taobao->getTitle(); $google->getTitle(); $runoob->getUrl(); $taobao->getUrl(); $google->getUrl();
The complete code is as follows:
Example
url = $par; } function getUrl(){ echo $this->url . PHP_EOL; } function setTitle($par){ $this->title = $par; } function getTitle(){ echo $this->title . PHP_EOL; } } $runoob = new Site; $taobao = new Site; $google = new Site; // 调用成员函数,设置标题和URL $runoob->setTitle( "菜鸟教程" ); $taobao->setTitle( "淘宝" ); $google->setTitle( "Google 搜索" ); $runoob->setUrl( 'www.runoob.com' ); $taobao->setUrl( 'www.taobao.com' ); $google->setUrl( 'www.google.com' ); // 调用成员函数,获取标题和URL $runoob->getTitle(); $taobao->getTitle(); $google->getTitle(); $runoob->getUrl(); $taobao->getUrl(); $google->getUrl(); ?>
Execute the above code and the output result is:
菜鸟教程 淘宝 Google 搜索 www.runoob.com www.taobao.com www.google.com
推荐学习:《PHP视频教程》
The above is the detailed content of What does php oop mean?. For more information, please follow other related articles on the PHP Chinese website!