In fact, there is no class and object in the world, code code, there are classes and objects.

Why use classes and objects?

Since the first snow in 2002… The application of IT in various fields is more and more, the program is more and more complex, there are a large number of repetitive or similar functions, resulting in a large number of function reuse, code reuse.

In order to reduce the complexity of the code, make the structure clearer and more efficient, more and more programmers began to extract the repeated part as a public module and abstract it. Some public attributes, public methods and public rules were abstracted, and these abstract contents were integrated, and the prototype of the class was formed.

On the basis of the class, according to different application scenarios, and then to do some adaptive adjustments, resulting in inheritance. Then the data of different application scenarios are imported to generate different instances.

Moreover, in the face of more and more complex business, the program is more and more coupled, and more and more difficult to maintain. Class meets the need to decouple, disassemble and reassemble programs. Programmers keep their jobs, and their hair.

And, with the more and more popular idea of object programming, the method of class, which abstracts things, has gained great favor and wide application.

What are classes and objects?

Classes are very simple. They extract and define the properties and functions of a class of things. The process of creating a class is the process of abstracting things.

Properties of things are expressed through parameters (constants, variables), and functions of things are expressed through methods (functions). This abstract definition is instantiated and embodied here, and “object” is produced.

For example, the “car” category, brand, size, weight, material, color, horsepower, fuel consumption, these are the attributes of the car, and forward, backward, navigation, steering, these are its functions. And BYD – Tang, Volvo -S80 is its example.

Classes are divided into: common class, abstract class, interface class, built-in class stdClass, subclass class extends, anonymous class, etc.

First, mediocre – Ordinary:

  1. You can instantiate and create objects.
  2. You can define various types of variables, constants, and methods.
  3. Can be inherited from a parent class, or can be inherited by multiple subclasses.

Let’s look at how to create a normal class.

HOW

Creating a normal class is simple:

`class Classname {

} `

This completes the creation of a class, which can be empty and represent nothing.

Once the class is created, we can use the new operator to instantiate the objects of that class.

$C= new Classname;

The class and object are created, but if you want it to make sense, you need to include the three basic elements of the class: member variables (attributes), member methods (functions), and class constants. Let’s look at it in detail.

attribute

Class variable members are called “properties”, which hold information data and interact with member methods (functions) to implement specific functions. We usually call attributes member variables.

Creating a class property is the first thing we do. The property value can be null or have an initial value, but the initialized value must be constant.

Continue extending the attributes in the above example:

Public $name; public $name; // Define a member variable, public visible public; public $height=180; Public $weight, $nationality; // You can define multiple variables protected $age in one statement; // Protected variable}Copy the code

There are two main ways to call a member variable, one is called in a member method, the other is called by object plus ->, for example:

echo $C->height; // 180, output attribute value;

echo $C->name='John'; // 'John', which is called by the object to assign a value to the attribute;

We can already do some basic things by defining properties. Notice that when we define properties, we add keywords like public, protected, private to indicate the visibility of the property.

Visibility of member variables:

  1. Public: fully exposed, accessible both inside and outside the class definition

  2. Private: can only be accessed inside the class. Cannot be accessed outside the class. Cannot be called or modified in a subclass

  3. Protected: Protected, accessible by itself and its subclasses and superclasses

  4. If nothing is written, the system defaults to public

When declaring an attribute, we can also restrict the data type of the attribute, for example:

class Classname { public string $name; // Restrict to string format public? Int $height //int null}Copy the code

A question mark is added before the data type to indicate that the variable can be int or NULL. If the variable is null, no error is reported. Note that null does not need to be assigned, but can be assigned null, null does not mean undefine.

methods

It is a function defined inside a class that is responsible for the implementation of specific functionality. It is often called a member method. Can be used to access and manipulate object data.

Creating a member method is similar to a function. Member methods are also visible. Like member variables, extend member methods based on the example above:

Public $name; public $name; // Define member variables that are publicly visible; public $height=180; Public $weight, $nationality; // You can define multiple variables protected $age in one statement; Public function player($name,$height, $weight, $age, $sex) {$this->name=$name; $this $this->weight=$weight; $this->height=$height; If ($this - > height < 185 && $this - > weight < = 85) {return $this - > name. ', meets the requirements; }else{return $this->name. }}}Copy the code

Calling a member method is similar to calling an attribute, in that the variable name is replaced by the method name and takes the specified argument:

$C=new Classname(); // Define an object with the keyword new

Echo $C->player(' male','male','male'); // The object calls a member method

From the above example, we can see that:

  1. Member variable definitions, which can have initial values, can define multiple variables in a single statement. You can also define variables with different visibility;
  2. The definition of a method is similar to that of a normal function. Inside a method, member variables can be referred to by the -> symbol.
  3. Call a variable in a member method using $this. It is a reference to the current object. In this example, it refers to the object $C, because the method is called by multiple objects, so $this is the pronoun of the calling object.
  4. Objects can be created using the new keyword;
  5. Methods can be called with object -> and take arguments;
  6. Public variables can be accessed via object ->;

When defining member method parameters, we also see a special type:

class Classname
{
    function obj(Classname $a): static {}    
}
Copy the code

This type declaration, which puts the class name in the parameter, requires that the variable that is put into the member method be the object of the specified class. Similarly, self indicates that the parameter must be an object of the method’s class.

Static means that the return value must be an object of the member method’s class. This syntax is available in PHP 8.0.

Today we saw that we created the object and then used the player() method to assign values to member variables. Is there a way to create an object and assign a value to a member variable at the same time? Sure, that’s what constructors are all about. We’ll do that in the next video.

One last thing I want to add about member methods, is there any difference between a member method and a normal function?

  1. An external function implements a separate function, while a member method implements a behavior in a class.

  2. Call: Functions can be called directly globally, while member methods need to be called by an object or through a class.

  3. Operation objects: External functions can operate globally and their internal variables, constants, and so on. Member methods can only operate on member variables of their own classes or call other member methods.

However, in some special situations, they can also call each other. Here’s an example, which I personally don’t recommend:

Function RRC ($RRC) // Define an external function {echo $RRC; } class Rrc {function BBC () // define a member method and reference the external function directly; { rrc('test'); } function VCC () // define a member method; { echo 'done'; } } $ccc=new Rrc; $ccc->bbc(); // Output test, implement the member method to the external function reference; Function DCC () // Defines an external function that refers to a class member method; { $GLOBALS['ccc']->vcc(); // we can refer to the object $CCC through $GLOBAL and call the method through the object; } dcc(); // Output done, the member method was successfully called;Copy the code

Well, the first basic lesson here, class ~!

Summary is not easy, do not reprint without permission, otherwise don’t blame old uncle you are welcome!

References:

www.php.net PHP official documentation

Zero foundation PHP learning Notes tomorrow technology