


How to define classes and their member attributes and operations in PHP_PHP Tutorial
Concept of class: A class is a collection of objects with the same attributes and operations. It provides a unified abstract description for all objects belonging to this class, which includes two main parts: attributes and operations. In object-oriented programming languages, a class is an independent program unit. It should have a class name and include two main parts: attribute description and operation description.
1. Class definition:
i. Class keyword definition uses class
1. Define an empty class
Class Person{};
2. Define a class with member attributes and operations
Class Person{
Member properties.....
Fuck...
}
3. Define a class that cannot be inherited and use the final keyword
Final class Person{
Member properties.....
Fuck...
}
4. Note: The final keyword cannot be used to modify member attributes, but can only modify classes and methods (final methods will be introduced later)
5. Here is a class with final
Define a final class FinalClass, containing a public function
final class FinalClass {
public function ffun() {
echo "This class is final class";
}
}
Define a class ChildFinalClass and inherit the FinalClass class
class ChildFinalClass extends FinalClass {
public function fchildfun() {
echo ‘This class inherits the final class FinalClass’;
}
}
In this way, when executing the above command, the system will prompt
Fatal error: Class ChildFinalClass may not inherit from final class (FinalClass)
Proves that classes defined by the final keyword cannot be inherited by subclasses
2. Definition of member attributes in the class
i. Member attributes are some variable attributes defined for the class. As a class, people have a pair of eyes (normal, except Erlangshen), a mouth, two ears, etc. Fixed proper nouns used to describe or express something are called member attributes
ii. Keywords used in the declaration of member attributes in a class
iii. Common member attribute declarations are composed of the following keywords
It starts with public, var, protected, private, and is followed by a variable. There are also some member attributes including static and constant const.
Public: Represents global, accessible to both internal and external subclasses of the class
Var: In the PHP 5 version, this member attribute will be considered a public type attribute
protected means protected and can only be accessed in this class, subclass or parent class
private
Indicates private and can only be used within this class
Static: 1) Static attributes,
2) Member properties modified with static can be shared by all objects of the same class
3) Static data exists in the data segment in memory when the class is loaded for the first time (initializing the static segment)
4) Use self::member attribute name in the class
Class name:: Member attribute name
Const: 1) Constant attributes in a class. When declaring constants in a class, you must use const2) Use SELF :: member attribute name
3) Use outside the class Class name::Member attribute name
Note: The variables in the attributes can be initialized, but the initialized value must be a constant. The constant here refers to the constant that the PHP script is in the compilation stage, not the constant calculated in the running stage after the compilation stage. For example, it cannot contain any operators, cannot be any variables, cannot be a function, etc.
In the member method of the class, you can access the properties and methods of the class through $this->property (property is the property name), but it cannot be used to access the static properties of the class or in the static method. , use self::$property instead. The pseudo variable $this can be used in non-static methods of a class. This pseudo variable is a reference to the instantiated object that calls the method.
Next, use code to explain the above content:
//static $a = strTolower();
//This writing is wrong
//const A = 1+2; //This writing is wrong
//public $name = 123+456;
//This writing is wrong
static $a = '$a';
const A = 'A';
public $name = 'Sister Feng';
protected $sex = 'male+female';
private $age = 23;
}
class ChildFinalClass extends FinalClass{
public function fchildfun(){
echo 'ChildFinalClass::$a->'.ChildFinalClass::$a."
";
Echo 'ChildFinalClass::A->'.ChildFinalClass::A."
";
echo 'Hello everyone, my name is'.$this->name."
";
echo 'I am:'.$this->sex.'生
';
echo 'My year:'.$this->age.'
';
//Since age is a private member attribute, it will not be called here, and it will be prompted that this attribute is not defined in the ChildFinalClass class.
}
}
$obj = new ChildFinalClass();
$obj->fchildfun();
3. Definition of operations in the class
i. I generally like to call operations as member methods. Below I will call operations as methods, but they are all the same
ii. Definition of member methods: Member methods are some function methods defined for the class. For example, take this class as an example. People can eat, run, and type code. These are member methods. That is to say, you can do some executable actions, which we understand as member methods
iii. For access to member methods and member attributes, please refer to the introduction to access to member attributes above.
iv. Member methods and member attributes also include public, protected, private, static, final and the scope is the same. Here are some examples for your reference and understanding.
v. Static member methods can only access static member properties and member methods, and you can use self::static method() to access static methods inside the class, and use class name::static method() to access externally the class
1. Custom method:
class FinalClass {
static $a = '$a';
const A = 'A';
public $name = 'Sister Feng';
protected $sex = 'male+female';
private $age = 23;
//Define a public method
public function publickfun(){
echo 'self::$a->'.self::$a."
"; in the FinalClass class
echo "self::A->".self::A."
"; in FinalClass class
echo 'Hello everyone, my name is:'.$this->name."
";
echo 'I am:'.$this->sex.'生
';
echo 'My year:'.$this->age.'
';
}
//Define a protected method
protected function protectedfun(){
echo 'self::$a->'.self::$a."
"; in the FinalClass class
echo "self::A->".self::A."
"; in FinalClass class
echo 'Hello everyone, my name is:'.$this->name."
";
echo 'I am:'.$this->sex.'生
';
echo 'My year:'.$this->age.'
';
}
//Define a private method
private function privatefun(){
echo 'self::$a->'.self::$a."
";
echo "self::A->".self::A."
"; in FinalClass class
echo 'Hello everyone, my name is:'.$this->name."
";
echo 'I am:'.$this->sex.'生
';
echo 'My year:'.$this->age.'
';
}
}
class ChildFinalClass extends FinalClass{
public function fchildfun(){
echo 'ChildFinalClass::$a->'.ChildFinalClass::$a."
";
Echo 'ChildFinalClass::A->'.ChildFinalClass::A."
";
echo 'I am:'.$this->sex.'生
';
echo 'My year:'.$this->age.'
';
//Since age is a private member attribute, it can be understood that I don’t want others to know my age, so it will not be called here, and it will prompt that this attribute is not defined in the ChildFinalClass class.
$this->publickfun();
$this->protectedfun();
$this->privatefun();
//Since privatefun is a private member method, it will not be called here.
}
}
$obj = new ChildFinalClass();
$obj->fchildfun();
2.Magic method
i. Magic methods must be defined as public, as must all other magic methods
ii. From PHP 5 and later, classes in PHP can use magic methods. It stipulates that methods starting with two underscores (__) are reserved as magic methods, so it is recommended that everyone’s function names should not start with __, unless it is to overload existing magic methods. Here are some magic methods. If If you want to know more about it, you can inquire about a certain one, so I won’t introduce it in detail here.
1. __construct() construction method
__destruct()
Destruction method
2. __clone()
If you want to copy an object, you need to use the clone method
3.__toString()
Themethod is automatically called when converting an object into a string, such as when using echo to print the object
4. __sleep() is used when serializing
__wakeup is called during deserialization
5. __set_state()
This static method will be called when var_export() is called (valid since PHP 5.1.0)
6. __invoke (valid for PHP 5.3.0 or above)
When trying to invoke an object as a function, the __invoke method is automatically called.
7. __callStatic (valid for PHP 5.3.0 and above) is to handle static method calls
8. __get() When an undefined attribute is called, this method will be triggered, and the parameter passed is the name of the attribute being accessed.
__set() This method will be triggered when assigning a value to an undefined attribute. The parameters passed are the attribute name and value to be set. The non-declaration here includes attributes whose access control is protected and private (that is, attributes that do not have permission to access) when called using an object.
9. __isset() This method is called when the isset() function is called on an undefined attribute
__unset() This method is called when the unset() function is called on an undefined property
10. __call( $method, $arg_array )
This method is called when calling an undefined method
The undefined methods here include methods that do not have permission to access; if the method does not exist, go to the parent class to find the method. If it does not exist in the parent class, call the __call() method of this class. If the __call() method does not exist in this class, go to the __call() method in the parent class.
11. __autoload() automatic loading magic method

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

CakePHP is an open source MVC framework. It makes developing, deploying and maintaining applications much easier. CakePHP has a number of libraries to reduce the overload of most common tasks.

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op
