Home > Backend Development > PHP Problem > How to define classes in php

How to define classes in php

青灯夜游
Release: 2023-03-15 13:38:01
Original
3809 people have browsed it

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

How to define classes in php

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

Definition of php class Method

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

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

The naming rules for class names, variable names and function names are similar, and they all need to follow the custom naming rules in PHP. Can be any legal tag that is not a PHP reserved word. 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: After an abstract class or method is 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:

Define an empty class

Class Person{};
Copy after login

Define a class with member properties and operations

Class Person{
  成员属性........
  操    作........
}
Copy after login

Define an empty class that cannot For inherited classes, use the final keyword

Final class Person{
  成员属性........
  操    作........
}
Copy after login

Note: The final keyword cannot be used to modify member properties, only classes and methods (final methods will be introduced later)

Here Take a class with final

Define a final class FinalClass, including a public function

final class FinalClass {
  public function ffun() {
  echo “本类为final类”;
  }
}
Copy after login

Define a class ChildFinalClass and inherit the FinalClass class

class ChildFinalClass extends FinalClass {
  public function fchildfun() {
  echo ‘本类继承final类FinalClass ’;
  }
}
Copy after login

Like this When executing the above command, the system will prompt

Fatal error: Class ChildFinalClass may not inherit from final class (FinalClass)
Copy after login

Proving that the class defined by the final keyword cannot be inherited by subclasses

Recommended study: "PHP Video Tutorial

The above is the detailed content of How to define classes 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