Home  >  Article  >  Backend Development  >  What is the method to create a class in php

What is the method to create a class in php

青灯夜游
青灯夜游Original
2021-10-13 17:45:323240browse

In PHP, you can use the class keyword plus the class name to create a class, and then use curly brackets "{}" to wrap the attributes and methods defined in the class body; the syntax format "[modification Class keyword] class class name {class attributes and methods;}".

What is the method to create a class in php

The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer

PHP supports object-oriented programming, supports classes and objects the concept of. From the perspective of data types, objects are a relatively special data type. It is generated by a predefined class, which is defined by the user. It consists of a series of data and several functions that operate on these data.

php creates (defines) a class

In PHP, you can use the class keyword plus the class name to define a class, and then use Curly brackets { } wrap the attributes and methods of the class defined in the class body. The syntax format of the class is as follows:

[修饰类的关键字] class 类名{
    类的属性和方法;
}

The naming rules for class names, variable names and function names are similar, and they all need to follow the rules in PHP Custom naming rules can be any legal tags that are not PHP reserved words. A legal class name starts with a letter or underscore, followed by a number of letters, numbers, or underscores. If the class name consists of multiple words, it is customary to capitalize the first letter of each word. In addition, the class name should have a certain meaning and should not be composed of just a few letters.

The keyword that modifies the class is an optional parameter and can be omitted. We usually use the following keywords to modify classes:

  • abstract: Abstract class or method. After being modified as an abstract class, the class cannot be instantiated, but can be inherited. If at least one method in a class is declared abstract, then the class must also be declared abstract. When inheriting an abstract class, the subclass must redefine all abstract methods in the parent class, and the access control of these methods must be the same as in the parent class.

  • final: Classes decorated with final cannot be inherited, and methods decorated with final cannot be redefined in subclasses.

Note: A class can contain its own constants, variables (called "member properties" or "properties" in the class) and functions (called "members" in the class method" or "method").

Example: Create a Students class and declare some member properties in the class. The code is as follows:

<?php
    class Students{
        var $name;
        public $age;
        private $sex;
        public static $school;
    }
?>

Tip: Permission modifiers can be mixed with the keyword static that defines static variables. use.

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of What is the method to create a class in php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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