Home > Article > Backend Development > PHP object-oriented simple summary
Basic knowledge
Class: A collection of classes with the same attributes or methods. For example, Chevrolet cars are a Chevrolet car category, Mercedes-Benz cars are a Mercedes-Benz car category, and BMW cars are a BMW car category, and these three categories are subcategories of the main car category.
Object: A specific implementation of the class. For example, the BMW Q5 is a specific implementation of the BMW car class. Objects are stored in memory. Let's take a look at the allocation of objects in memory.
Using object-oriented means to use the above two knowledge points flexibly. Let us create and use classes and objects
<?php /** *新建一个类 */ class newClass{ public $a; public $b; public function funA(){ echo "I am function A"; } public function funB(){ echo "I am function B"; } } /** *使用类创建对象 */ $opt=new newClass(); $opt->a="opt";//将opt对象中的a属性赋值为opt $opt->funA();//打印出"I am function A"?>
Modifiers: When defining classes and attributes in the class, we can use the following three modifiers. If not added, the default is public
public: Maximum permissions
protected: The scope of permissions is within itself and its subclasses
private: The scope of permissions is only within itself
Constructor and destructor: When we instantiate a class to create an object, we often need to initialize the object. At this time, we need to define a constructor method in the class. When we are done using the object, we need to release the object to reduce memory usage. At this time we need to use the destructor method.
In php we use __construct() method and __destruct(), the following code
<?php class newClass{ public $a; public $b; public function __construct($a,$b){ $this->a=$a; $this->b=$b; echo "我是构造函数"; } public function __destruct(){ echo "我是析构函数"; } } ?>
Encapsulation
When we When developing some important program modules, we often do not want others to easily access the data of these program modules, so these data need to be encapsulated. At this time we need to perform data access control, often using private keyword to encapsulate these properties and methods.
As follows:
<?php class privateClass { private $a; private $b; private function privateFun() { echo "我是封装的方法"; } } ?>
In this way, when we create an object, we cannot call private properties and methods. But we can access these private properties and methods by using magic methods.
Use of __set() and __get()
Through the __set() method we can directly set the member attribute values through the object
Through the __get() method we can directly obtain the member attribute values through the object
<?php class setClass { private $a; private $b="ww"; public function __set($oldValue,$newvalue){ $this->$oldValue=$newvalue; } public function __get($newvalue){ return $newvalue; } } $opt= new setClass(); $opt->a="sss";//直接设置私有属性值 echo $opt->b;//直接读取私有属性值 ?>
Use of __isset() and __unset()
Directly check whether the private attributes in the object exist through __isset()
Directly delete the private attributes in the object through __unset()
<?php class issetClass { private $a; private $b = 'www'; function __isset($privateName) { if (isset($privateName)) { return 1; } else { return 0; } } function __unset($privateName) { echo "销毁" . $privateName; } } $opt = new issetClass(); echo isset($opt->$b); var_dump(unset($opt->$b));?>
Inheritance
When we write multiple classes, often multiple classes have the same properties and methods. In order to simplify the code we introduce inheritance The concept of subclasses can inherit some attributes and methods of the parent class, reducing redundant code writing.
How to write inheritance classes
We use the keyword extends to write subclasses
<?php class parentClass{ } class childClass extends parentClass{ } ?>
As above, childClass is a subclass of parentClass. PHP only supports single inheritance, that is, there is only one subclass. Parent class, but a parent class can have multiple subclasses.
Override the parent class method
When the method in the parent class cannot satisfy the use of the subclass, we can override the parent class method. But when we want to use the method of the parent class in the subclass, we can use the following syntax: parent:: method name. Several important keywords
3.1 final
final can modify the class With methods, member attributes cannot be modified;
Classes modified by final cannot be inherited, and methods modified with final cannot be overridden in subclasses
3.2 static
Static can modify member properties and member methods, but cannot modify classes;
Members and methods modified with static can be used directly through the class, using the following syntax: class name::\$property name or method name();
When used in a class, use the following syntax: self::\$ attribute name or method name.
3.3 const
Use const to declare constants in a class instead of define( );
For example, const TT=90, when using a constant, use the following syntax directly self::constant name
3.4 instanceof
Use this keyword to detect whether an instance is Is an instance of a class.
3.5 trait
php can only perform single inheritance, but it also provides an alternative way to reuse code and solve the problem of single inheritance.
As follows
<?php trait testA { function a() { } } trait testB { function b() { } } class testC { use testA, testB; function c() { } } ?>
Abstract technology
Methods and classes modified with the abstract keyword are called abstract methods or abstract classes .
Declaring abstract classes and abstract methods
<?php abstract class testClass { $a; $b; abstract function testFun(); abstract function testFun1(); public function optFun() { echo "抽象类中的抽象方法"; }//抽象类可以有非抽象方法 } class childClass extends testClass { function testFun() { echo "子类中实现抽象方法"; } function testFun1() { echo "子类实现抽象方法"; } } abstract child1Class extends testClass { }//子类是抽象方法可以不实现父类的抽象方法?>
Abstract technology provides a specification for the declaration of subclasses, limiting the instantiation of the class (abstract classes cannot be instantiated).
Interface technology
Interface is a special abstract class. Only abstract classes and constants can be declared in the interface
Declaration of interface usage interface, implement the interface using implements, the modifier can only be the default public;
A subclass can inherit multiple interfaces and inherit a parent class at the same time
<?php interface newInterface { const V = 12; abstract function fun1(); abstract function fun2(); } interface newInterface1 { abstract function fun3(); abstract function fun4(); } class parentClass { function fun5() { echo "fun5"; } } class optClass extends parentClass implements newInterface, newINterface1 { function fun1() { echo "fun1"; } function fun2() { echo "fun2"; } function fun3() { echo "fun3"; } function fun4() { echo "fun4"; } } ?>
Polymorphism
Polymorphic implementation in PHP requires a class to be inherited through multiple subclasses. If a class method is rewritten in multiple subclasses and implements different functions, we call it polymorphism.
Recommended tutorial: "PHP Tutorial"
The above is the detailed content of PHP object-oriented simple summary. For more information, please follow other related articles on the PHP Chinese website!