Object-oriented programming (ObjectOrientedProgramming, OOP, object-oriented programming) is a computer programming architecture. One basic principle of OOP is that a computer program is composed of a single unit or object that can act as a subroutine. OOP achieves three goals of Software Engineering: reusability, flexibility, and scalability. The following is the body of the article:
Basic concepts
Object-Oriented Programming (OOP) is a computer Programming architecture. One basic principle of OOP is that a computer program is composed of a single unit or object that can act as a subroutine. OOP achieves three goals of Software Engineering: reusability, flexibility, and scalability.
PHP later than version 4.0 has improved support for OOP. For small applications, traditional procedural programming may be simpler and more efficient. However, for large and complex applications, OOP is an option that must be considered.
Class
A class is a set of objects with the same attributes and services. It provides a unified abstract description for all objects belonging to this class, which includes two main parts: attribute and service. In an object-oriented programming language, a class is an independent program unit. it should have a class name and contain two main parts: attribute description and service description.
Object
An object is an entity used to describe objective things in a system. it is a basic unit of a system. An object consists of a group of attributes and a group of services that operate on these attributes.
The relationship between a class and an object is like the relationship between a mold and a casting. The instantiation result of a class is an object, and the abstraction of a class object is a class.
Object-oriented programming involves a wide range of content. this tutorial only introduces basic concepts and applications in PHP.
Class
Use the keyword class to declare a class, followed by the class name, and the subject is enclosed by the {} symbol.
Syntax:
class class_name{ ...... }
Class contains attributes and methods.
Attribute
In the class body, special variables called attributes can be declared. In PHP V4, attributes must be called with the keyword var. This is still a legal syntax, but mainly for backward compatibility. In PHP V5, attributes must be declared as public, private, or protected. Keyword: can we have a bit of privacy here? . However, in this example, all attributes are declared as public. Listing 1 shows a class with two attributes declared.
Classes that declare two attributes
class Dictionary { public $translations = array(); public $type ="En";}
For example, if a person's class is defined, the person's name, age, gender, and so on can be regarded as the attributes of the person class.
Method
The class method is created by declaring a function in the class definition.
Syntax:
Class class_name {function function_name (arg1, arg2 ,......) {Function code }}
Class applications
A class that defines attributes and methods is a complete class. it can contain a complete processing logic in a class. Use the new keyword to instantiate an object to apply the logic in the class. Multiple objects can be instantiated at the same time.
Syntax:
object = new class_name();
After an object is instantiated, use the-> operator to access the member attributes and methods of the object.
Syntax:
object->var_name; object->function_name;
To access the attributes or methods of a member in a defined class, you can use the pseudo variable $ this. $ This indicates the current object or the object itself.
Example:
Name ."
"; Echo" My age is :". $ this-> age ;}// class definition ends // instantiate an object $ p1 = new Person (); // assign $ p1 object attributes $ p1-> name = "zhang san"; $ p1-> age = 20; // call the say () in the object () method $ p1-> say ();?>
Run this example and output:
My name is Zhang San. my age is: 20.
The above example demonstrates a simple object-oriented PHP application.
The above is (Advanced article) PHP object-oriented-class and object content. For more information, see PHP Chinese website (www.php1.cn )!