PHP Beginner's Introduction to Object Orientation
What is object-oriented
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
Object-oriented programmingreferred to as OOP
In the real society, the things we face are objects, such as computers, cups, houses, etc.
The three main characteristics of objects
1. Object behavior: What operations can be applied to the object, turning on the light and turning off the light are behaviors.
2. The shape of the object: How the object responds, color, size, and appearance when those methods are applied.
3. Representation of objects: The representation of objects is equivalent to an ID card, specifically distinguishing the differences between the same behaviors and states
Object-oriented content
1.Class: Defines the abstract characteristics of a thing. The definition of a class includes the form of data and operations on data
2.Object: It is an instance of the class
3.Member variable: 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 be called an attribute of the object
4.Member Function: Defined inside the class and can be used to access the object's data
5.Inheritance: Inheritance is a mechanism for subclasses to automatically share parent class data structures and methods. This is the a relationship between. When defining and implementing a class, you can do it on the basis of an existing class, use the content defined by the existing class as your own content, and add some new content
6.Parent class: A class is inherited by other classes. This class can be called a parent class, base class, or super class
7. Subclass: A class that inherits other classes is called a subclass, or it can also be called a derived class
8.Polymorphism: Polymorphism refers to the same operation or function, Procedures can operate on many types of objects and achieve different results. Different objects can produce different results after receiving the same message. This phenomenon is called polymorphism
9.Overloading: Simply put, it means that functions or methods have the same name , but the parameter lists are different, such functions or methods with the same name and different parameters are called overloaded functions or methods
10.Abstractness: Abstraction is It refers to abstracting objects with consistent data structures (properties) 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
11.Encapsulation: Encapsulation refers to binding the properties and behavior of an object that exists in the real world together and placed in a logical unit
12.Constructor; is mainly used to initialize the object when creating the object, that is, assign initial values to the object member variables, always with the new operation symbols are used together in statements that create objects
13.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)
Definition of class:
#1. A class is defined using the class keyword followed by the class name.
2. Variables and methods can be defined within a pair of braces ({}) after the class name.
3. Class variables are declared using var, and variables can also be initialized.
4. The function definition is similar to the definition of the PHP function, but the function can only be accessed through the class and its instantiated objects
<?php class Site { /* 成员变量 */ var $url; var $title; /* 成员函数 */ function setUrl($par){ $this->url = $par; } function getUrl(){ echo $this->url; } function setTitle($par){ $this->title = $par; } function getTitle(){ echo $this->title; } } //注:变量 $this 代表自身的对象。 ?>
Objects created in php
After the class is created, we can use the new operator to instantiate the object of the class
$php = new Site;
$taobao = new Site;
$google = new Site;
We created three objects in the above code, each of the three objects is independent
<?php header("Content-type: text/html; charset=utf-8");//设置编码 class Site { /* 成员变量 */ var $url; var $title; /* 成员函数 */ function setUrl($par){ $this->url = $par; } function getUrl(){ echo $this->url . "</br>"; } function setTitle($par){ $this->title = $par; } function getTitle(){ echo $this->title . "</br>"; } } $php = new Site; $taobao = new Site; $google = new Site; // 调用成员函数,设置标题和URL $php->setTitle( "php中文网" ); $taobao->setTitle( "淘宝" ); $google->setTitle( "Google 搜索" ); $php->setUrl( 'm.sbmmt.com' ); $taobao->setUrl( 'www.taobao.com' ); $google->setUrl( 'www.google.com' ); // 调用成员函数,获取标题和URL $php->getTitle(); $taobao->getTitle(); $google->getTitle(); $php->getUrl(); $taobao->getUrl(); $google->getUrl(); ?>
Constructor
Constructor is a special method. It is mainly used to initialize the object when creating the object, that is, to assign initial values to the object member variables. It is always used together with the new operator in the statement that creates the object.
PHP 5 allows developers Define a method as a constructor in a class. The syntax format is as follows:
void __construct ([ mixed $args [, $... ]] )
Analysis Constructor
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
PHP 5 introduced the concept of destructor, which is similar to other object-oriented languages. Its syntax format is as follows: void __destruct (void)
<?php header("Content-type: text/html; charset=utf-8");//设置编码 class MyDestructableClass { function __construct() { print "构造函数</br>"; $this->name = "MyDestructableClass"; } function __destruct() { print "销毁 " . $this->name; } } $obj = new MyDestructableClass(); ?>
Inheritance
PHP uses the keyword extends to inherit a class. PHP does not support multiple inheritance. The format is as follows:
class Child extends Parent {
Also called
overridingof a method.
The getUrl and getTitle methods are overridden in the example: <?php
// 子类扩展站点类别
class Child_Site extends Site {
var $category;
function setCate($par){
$this->category = $par;
}
function getCate(){
echo $this->category;
}
}
?>
Access control
public (Public): Public class members can be accessed from anywhere.
protected (Protected): Protected class members can be accessed by itself and its subclasses and parent classes.
private (Private): Private class members can only be accessed by the class in which they are defined.
Access control of attributes
Class attributes must be defined as one of public, protected, and private. If defined with var, it is regarded as public
<?php function getUrl() { echo $this->url . PHP_EOL; return $this->url; } function getTitle(){ echo $this->title . PHP_EOL; return $this->title; } ?>
Access control for methods
Methods in a class can be defined as public or private or protected. If these keywords are not set, the method defaults to public
<?php /** * Define MyClass */ class MyClass { public $public = 'Public'; protected $protected = 'Protected'; private $private = 'Private'; function printHello() { echo $this->public; echo $this->protected; echo $this->private; } } $obj = new MyClass(); echo $obj->public; // 这行能被正常执行 //echo $obj->protected; // 这行会产生一个致命错误 //echo $obj->private; // 这行也会产生一个致命错误 $obj->printHello(); // 输出 Public、Protected 和 Private /** * Define MyClass2 */ class MyClass2 extends MyClass { // 可以对 public 和 protected 进行重定义,但 private 而不能 protected $protected = 'Protected2'; function printHello() { echo $this->public; echo $this->protected; //echo $this->private; } } $obj2 = new MyClass2(); echo $obj2->public; // 这行能被正常执行 //echo $obj2->private; // 未定义 private //echo $obj2->protected; // 这行会产生一个致命错误 $obj2->printHello(); // 输出 Public、Protected2 和 Undefined ?>
Constant
can be placed in the class A value that always remains the same is defined as a constant. There is no need to use the $ symbol when defining and using constants.
The value of a constant must be a fixed value and cannot be a variable, class attribute, the result of a mathematical operation or a function call.
Since PHP 5.3.0, you can use a variable to dynamically call a class. But the value of this variable cannot be a keyword (such as self, parent or static)
Abstract class
Any class if it If at least one method is declared abstract, then the class must be declared abstract.
Classes defined as abstract cannot be instantiated.
A method defined as abstract only declares its calling method (parameters) and cannot define its specific function implementation.
When inheriting an abstract class, the subclass must define all abstract methods in the parent class; in addition, the access control of these methods must be the same (or more relaxed) as in the parent class. For example, if an abstract method is declared as protected, then the method implemented in the subclass should be declared as protected or public, and cannot be defined as private. In addition, the method calling method must match, that is, the type and number of required parameters must be consistent. For example, if the subclass defines an optional parameter, but it is not included in the declaration of the abstract method of the parent class, there is no conflict between the two declarations
Interface
Using interface (interface), you can specify which methods a certain class must implement, but you do not need to define the specific content of these methods.
The interface is defined through the interface keyword, just like defining a standard class, but all methods defined in it are empty.
All methods defined in the interface must be public. This is a characteristic of the interface.
To implement an interface, use the implements operator. The class must implement all methods defined in the interface, otherwise a fatal error will be reported. A class can implement multiple interfaces. Use commas to separate the names of multiple interfaces.