Home > Backend Development > PHP Tutorial > In-depth understanding of the concept of PHP class_PHP tutorial

In-depth understanding of the concept of PHP class_PHP tutorial

WBOY
Release: 2016-07-21 15:18:15
Original
991 people have browsed it

For example, a vehicle can be defined with properties such as color, number of tires, manufacturer, model, and capacity, and behaviors such as stopping, moving forward, turning, and honking. In OOP terminology, a specific definition of the properties and behavior of an entity is called a class.

Definition and creation of classes
A class is a collection of objects with the same properties and services. It provides a unified abstract description for all objects belonging to this class, which includes two main parts: attributes and methods. In object-oriented programming languages, a class is an independent program unit. It should have a class name and include two main parts: attribute description and method description. The

class is used to represent the actual things to be handled in the application. For example, if you were creating an application to manage a public library, you might include classes to represent books, magazines, staff, special events, patrons, and other things that need to be managed. Each entity contains a set of properties and behaviors, called fields and methods in OOP, which define the entity. The general class creation syntax in PHP is as follows:

Copy the code The code is as follows:

class Class_Name
{
// Field declaration
// Method declaration
}

Create a class:
Copy code The code is as follows:

class Employee
{
private $name;
private $title;
protected $wage;

protected function clockIn( ) {
echo "Member $this->name clocked in at ".date("h:i:s");
}

protected function clockOut() {
echo "Member $this->name clocked out at ".date("h:i:s");
}
}

This class is called Employee and defines 3 There are three fields: name, title and wage, and two methods are defined: clockIn (check-in) and clockOut (check-out).

Application of classes
A class that defines attributes and methods is a complete class, and a complete processing logic can be included in a class. Use the new keyword to instantiate an object in order to apply logic within the class. Multiple objects can be instantiated simultaneously.

Instantiation of class:
Copy code The code is as follows:

object = new class_name();

After instantiating an object, use the -> operator to access the object's member properties and methods. For example:
Copy code The code is as follows:

object->var_name;
object->function_name;

If you want to access the properties or methods of members in the defined class, you can use the pseudo variable $this. $this is used to represent the current object or the object itself.
Copy code The code is as follows:

class Person {
// members of person Attribute
var $name; //Person’s name
var $age; //Person’s age

//Person’s members say() method
function say() {
echo "My name is:".$this->name."
";
echo "My age is:".$this->age;
}
}
//End of class definition

$p1 = new Person(); //Instantiate an object
$p1->name = "Gonn"; //Give $p1 Object attribute assignment
$p1->age = 25;
$p1->say(); //Call the say() method in the object
?>

Program running results:
Copy code The code is as follows:

My name is: Gonn
My age Yes: 25

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/325556.htmlTechArticleFor example, a vehicle can be defined with properties such as color, number of tires, manufacturer, model and capacity, and define There are behaviors such as stopping, moving forward, turning, and honking. In OOP terms, entities...
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template