search
HomeBackend DevelopmentPHP ProblemWhat is the method to create a class in php

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment