The difference between classes and objects in php: Classes are the abstraction of objects, and objects are concrete instances of classes; classes are abstract and do not occupy memory, while objects are concrete and occupy memory space. For example: the class is the fruit and the object is the apple.
The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer
What is a class?
Class is the basic concept of object-oriented programming. It is a description of a class of things. The popular understanding of a class is the abstraction of a certain type of thing in reality. The definition of a class includes the data forms and operations on data. Is a collection of objects that share some of the same properties and behaviors.
What is an object?
Objects are instances of classes. Objects are concrete, and our classes are abstract
The relationship and difference between classes and objects
The instantiation result of a class is an object, and for a class of objects The abstraction is the class. A class describes a group of objects with the same characteristics (properties) and the same behavior (methods).
A class is an abstraction of an object, and an object is a concrete instance of a class. Classes are abstract and do not occupy memory, while objects are concrete and occupy memory space. For example: the class is fruit and the object is apple.
How to define a class?
The declaration of a class is declared through the class keyword (PS: the class name begins with a capital letter). The syntax rules are as follows:
class 类名{ }
Instantiating a class is to create an object
Instantiate through the keyword new
, the syntax is as follows:
$对象变量 = new 类名称();
For example, we declare a car class and instantiate it.
//声明一个汽车类 class Car { //属性 public $name = '汽车'; //方法 public function run(){ return 'run'; } } //实例化Car $car = new Car();
Attributes
Attributes are member variables of the class and are common attributes in the class
The declaration of the attribute must Modify through the modifiers public, private, and protected keywords
The declaration does not need to set an initial value, and cannot be an expression, function, object, or resource type
Methods
Methods are some functional behaviors of the class. It is dynamically executable. By declaring a function in a class definition, you create a method of the class.
The method can be declared without modifiers. If not, it will be declared as public.
[Recommended learning: "PHP Video Tutorial"]
Use of class attributes and methods
1. Class internal access
Access through keyword $this
->
operator
Access attribute: $this-> attribute; //$this->name;
Access method: $this-> method; //$this->run ();
2. Access the
instantiated class through a reference variable (object) and access it through the ->
operator .
$car = new Car(); $car->name; //访问属性 $car->run(); //访问方法
Comparison of objects
1. Use comparison operator (==)
When two objects have the same attributes and values, they belong to the same If a class is defined in the same namespace, the two objects are equal
2. Use the congruent symbol (===)
The two object variables must point to a certain class The same instance (that is, the same object)
For more programming-related knowledge, please visit: Programming Video! !
The above is the detailed content of What is the difference between classes and objects in php. For more information, please follow other related articles on the PHP Chinese website!