Object-oriented programming (OOP) basics of PHP learning records [classes, objects, inheritance, etc.]

This article describes the object-oriented (Object-oriented programming, OOP) foundation of PHP learning records with examples. Share it with everyone for your reference, as follows: In object-oriented programming (English: Object-oriented programming, abbreviation: OOP), an object is a whole composed of information and a description of information processing, and is an abstraction of the real world. The things we face in the world are objects, such as computers, televisions, bicycles, etc. Let’s look at the three main characteristics of objects: Behavior of the object: those 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 when those methods are applied, color, size, shape. Representation of the object: The representation of the object is equivalent to an ID card, and the specific distinction is the difference between the same behavior and state. For example, Animal (animal) is an abstract class, we can specify a dog and a sheep, and dogs and sheep are specific objects, they have color attributes, can write, can run and other behaviors Status, look at this picture to feel: Look at the main content of object-oriented: Class − defines the abstract characteristics…

Learning of the three major characteristics of PHP object-oriented (full understanding of abstraction, encapsulation, inheritance, polymorphism)

The three characteristics of object-oriented: encapsulation, inheritance, and polymorphism First, let’s briefly understand the abstraction:When we defined a class earlier, we actually extracted the common attributes and behaviors of a class of things , forming a physical model (template), this method of researching problems is called abstraction 1. EncapsulationEncapsulation is to encapsulate the extracted data and the operation on the data together, the data is protected inside, and other parts of the program only have authorized operations (methods ) to operate on the data. php provides three access control modifierspublic means global, inside this class, outside the class, and subclasses can accessprotected means protected, only this class or subclasses can accessprivate means private, only accessible inside this class , there is also this way of writing var $name in PHP4, which means public attributes, and this way of writing is not recommendedExample: The code is as follows: < ?php class Person{ public $name; protected $age; private $salary; function __construct($name,$age,$salary){ $this ->name=$name; $this->age=$age; $this->salary=$salary; } public function showinfo(){ //this Indicates that all three modifiers can be used inside this class. echo $this->name.”||”.$this->age.”||”.$this->salary; } } $p1=new Person(‘Zhang San’,20,3000); //This is outside the class, so if you use the following method to access…

Learning the three major characteristics of PHP object-oriented (full understanding of abstraction, encapsulation, inheritance, polymorphism)_PHP Tutorial

The three characteristics of object-oriented: encapsulation, inheritance, and polymorphism First, let’s briefly understand the abstraction:When we defined a class earlier, we actually extracted the common attributes and behaviors of a class of things , forming a physical model (template), this method of researching problems is called abstraction 1. EncapsulationEncapsulation is to encapsulate the extracted data and the operation on the data together, the data is protected inside, and other parts of the program only have authorized operations (methods ) to operate on the data. php provides three access control modifierspublic means global, inside this class, outside the class, and subclasses can accessprotected means protected, only this class or subclasses can accessprivate means private, only accessible inside this class , there is also this way of writing var $name in PHP4, which means public attributes, and this way of writing is not recommendedExample: The code is as follows: <?php class Person{ public $name; protected $age; private $salary; function __construct($name ,$age,$salary){ $this->name=$name; $this->age=$age; $this->salary=$salary; } public function showinfo(){ //This means that all three modifiers can be used inside this classecho $this->name.”||”.$this->age.”||” .$this->salary; } } $p1=new Person(‘Zhang San’,20,3000); //This is outside the class, so if you use the following method Both age and…

Three object-oriented features of PHP: encapsulation, inheritance, polymorphism

Algorithms increase the speed of program execution, while design patterns increase the speed of programming. PHP is an object-oriented scripting language , and we all know ,Object-oriented language has three major characteristics& #xff1a;encapsulation,inheritance,polymorphism. Abstract classes and interfaces Abstract/Abstraction As long as there is one abstract method in a class ,This class must be declared abstract. Abstract methods must be overridden in subclasses. In fact, abstract classes and interface classes are partly similar , remember where I saw such a sentence , abstract class extracts the part of the class like , Such as ,whale and carp, these two things from the perspective of animals , have a clear distinction boundary , belong to different animals ; but in aquatic animals In terms of perspective,they belong to the same animal abstraction. A reasonable abstraction of the problem & # xff0c; construction model & # xff0c; will make it easier to solve the problem through programming. Remember :Abstraction is the basis of programming to solve problems,the more complicated the problem,the more you need to abstract the problem from the beginning,instead of writing code directly . Abstract class is a programming concept , called Abstract Classes in PHP. In the design pattern &…

Object-oriented programming (OOP) basics of PHP learning records [classes, objects, inheritance, etc.]

This article describes the object-oriented (Object-oriented programming, OOP) foundation of PHP learning records with examples. Share it with everyone for your reference, as follows: In object-oriented programming (English: Object-oriented programming, abbreviation: OOP), an object is a whole composed of information and a description of information processing, and is an abstraction of the real world. The things we face in the world are objects, such as computers, televisions, bicycles, etc. Let’s look at the three main characteristics of objects: Behavior of the object: those 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 when those methods are applied, color, size, shape. Representation of the object: The representation of the object is equivalent to an ID card, and the specific distinction is the difference between the same behavior and state. For example, Animal (animal) is an abstract class, we can specify a dog and a sheep, and dogs and sheep are specific objects, they have color attributes, can write, can run and other behaviors Status, look at this picture to feel: Look at the main content of object-oriented: Class − defines the abstract characteristics…

Contact Us

Contact us

181-3619-1160

Online consultation: QQ交谈

E-mail: [email protected]

Working hours: Monday to Friday, 9:00-17:30, holidays off

Follow wechat
Scan wechat and follow us

Scan wechat and follow us

Follow Weibo
Back to top
首页
微信
电话
搜索