Home > Backend Development > PHP Problem > What is the difference between classes and objects in php

What is the difference between classes and objects in php

青灯夜游
Release: 2023-03-08 08:12:01
Original
3140 people have browsed it

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.

What is the difference between classes and objects in php

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 类名{
}
Copy after login

Instantiating a class is to create an object

Instantiate through the keyword new, the syntax is as follows:

$对象变量 = new 类名称();
Copy after login

For example, we declare a car class and instantiate it.

//声明一个汽车类
class Car {
    //属性
    public $name = '汽车';
    //方法
    public function run(){
        return 'run';
    }
}

//实例化Car
$car = new Car();
Copy after login

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(); //访问方法
Copy after login

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!

Related labels:
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