Home  >  Article  >  Backend Development  >  What are the features of php oop? Introduction to the three major characteristics of php oop thinking

What are the features of php oop? Introduction to the three major characteristics of php oop thinking

不言
不言Original
2018-07-25 14:10:306990browse

The three major characteristics of php oop thought are: encapsulation, inheritance and polymorphism. Here, I will give you a detailed understanding of php opp thought. Then, let’s take a detailed look at it. The three major characteristics of opp thought and php opp thought.
Encapsulation

Encapsulation is to combine the properties and behavior of an object into an independent unit.
Encapsulating a class requires two steps. The first step is to privatize a class. The second step is to use set and get to read and assign values.
The advantage is that it hides the implementation details of the class and can easily add logic. Controllability, restricting unreasonable operations on attributes, making it easy to modify and enhance the maintainability of the code.

__get and __set
Generally speaking, it is more in line with realistic logic to make the class private.
Two predefined functions are used to obtain and apply values.
__get The value obtained is usually the value of the domain
__set The value set is usually the value of the domain
__call When a method that does not exist in an object is called, an error will occur. call() is the method to handle this situation. .

Static properties and methods
static keyword to declare static methods
static static variable generates a static variable inside the class, which can be shared by all classes. In other words, static members are Put it in the "Initialization Static Segment", which is put in when the class is loaded for the first time, so that it can be shared by every object in the heap memory.
Usage: self::$static properties, self::static methods

static function p(){
echo self::$country;
echo self::PI;//访问常量
//echo $this->name;在静态方法中只能操作静态属性
//self::p();
}

External calls: Class::$static properties, Class::static methods

const keyword: used to generate constants Constants are the only convention that cannot be changed Constants are in uppercase letters
const CONSTANT = 'constant value'; Generate a constant
echo self::CONSTANT;//Internal class access
echo ClassName::CONSTANT;//External class access

Inheritance
Objects of class B have all the properties and behaviors of class A, which is called B’s inheritance of class A.
If a class inherits properties and services from multiple classes, this is called multiple inheritance. Usually we call the inheriting class a subclass and the inherited class is a parent class. In PHP, there is only single inheritance, but a parent class can Inherited by multiple classes, but a subclass can only have one parent class, but associated inheritance is allowed, and the definition of the class can be reduced through inheritance.
extende declares inheritance relationship
Syntax format: class B extends A This example indicates that B inherits A
External access of class is valid for subclasses
Attributes and methods of subclasses and parent classes
The subclass inherits all the contents of the parent class, but the private part in the parent class cannot be directly accessed
The newly added attributes and methods in the subclass are extensions to the parent class
The names defined in the subclass have the same name as the parent class The attribute is an override of the parent class attribute, and the method with the same name is also an override of the parent class method

Overridden method
In the subclass, use parent to access the overridden attributes in the parent class and Method
parent::__construce();
parent::$name;
parent::fun();

Overwrite the original attributes of the parent class
clone object syntax Format $c=clone $p; $c's object $p outputs echo $c->name;

Object comparison
===Two comparison operators.
== is to compare the contents of two objects.
=== is the handle of the comparison object, that is, the reference address.

The instanceof operator is used to detect whether the object belongs to a certain class. If the type belongs to it, it returns true. If it does not, it returns false.
__clone() If you want to change the content of the original object after cloning, you need to use __clone( ) to rewrite the original properties and methods

function __clone(){
$this->name="我是一个克隆人";
}

final means that a class is the final version, which means that it cannot be called by subclasses

Polymorphism

Polymorphism means that after attributes or behaviors defined in the parent class are inherited by the subclass, they can have different data types or show different behaviors. This allows the same property or behavior to have different semantics in the parent class and its various subclasses.
That is to say, the results of executing the same method in the subclass and the parent class are different.

class A {
function info(){
echo “A INFO”;
}
}
class B extends A {
function info(){
echo “B INFO”;
}
}
class C extends A {
function info(){
echo “C INFO”;
}
}
function printinfo($obj){
function printinfo(A $obj){
if($obj instanceof A)
$obj->info();
$obj->info();
}
}
$a=new A(); $b=new B(); $c=new C();
printinfo($a); //输出A INFO
printinfo($b); //输出B INFO
printinfo($c); //输出C INFO

Abstract methods and abstract classes

Abstract methods are used as templates for subclasses.

abstract class Person{
public $name;
abstract function getInfo();
}

Abstract classes cannot be used. In an abstract class, there must be an abstract method. But dynamic functions can be defined in abstract classes.
Interface
When a class inherits an interface, it must override all methods of the interface. The interface can only declare constants. The methods of the interface must be defined as shared or they cannot be inherited. Interfaces can inherit from multiple interfaces
Grammar:

interface PCI{
const TYPE="PCI";
//public $name; error
function start();
function stop();
}
接口中的方法可以声明为static
interface A{ function a();}
interface B{ function b();}
interface C extends A{ function c();}
class D implements B,C{
function a(){}
function b(){}
function c(){}
}

Related recommendations:

Introduction to PHP OPP mechanisms and patterns (abstract classes, interfaces and contract programming)_php examples

The above is the detailed content of What are the features of php oop? Introduction to the three major characteristics of php oop thinking. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn