PHP series (1)

黄舟
Release: 2023-03-05 12:18:02
Original
1724 people have browsed it

Write at the beginning of the article:

The Spring Festival is approaching and I will go home tomorrow. I am extremely excited. After a busy year, I can finally spend time with my family. After outsiders left Beijing, Beijing was like an "empty city". When my colleagues were talking about this today, they suddenly remembered Sima Qian's famous saying: "Everything in the world is bustling for profit; the world is bustling, everything is for profit." Now the direction of profit is It should be about family ties, winding up the kite, no matter what you have experienced in the past year, you must pack up your mood and go home to celebrate the New Year. I wish you all a Happy New Year in advance~

During the Spring Festival, I will also fulfill my promise to myself and publish at least one article every week.

I would also like to thank And1 readers for their valuable suggestions. If you have any good suggestions, you can also tell me.

Return to the main text: Today we will talk about classes in PHP.

1 What is a class

A class is to put some things into one category, such as cats, dogs, and pigs. They are all animals, so we can put them into one category. Classes are abstract and not concrete. If a class is made concrete, it is an object of the class. Cats and dogs are objects in the animal class. Students can also be regarded as a class, then Xiaohong is an object in the student class (the terminology in development is also called 'instance')

Roughly speaking, a class is a collection of many methods (actually it is not There are attributes, which are only implemented in conjunction with methods). These methods are some logic or algorithms that you often use in programs. Wrapping them into classes can improve the efficiency of the program and reduce code duplication.

2 Definition of class

The definition of each class starts with the keyword class, followed by the class name, followed by a pair of curly brackets, which contains the attributes and attributes of the class Definition of method.

<?php
class SimpleClass
{
    // 声明类的属性
    public $var = &#39;a default value&#39;;

    // 声明类的方法
    public function displayVar() {
        echo $this->var;
    }
}
?>
Copy after login

When a method is called inside a class definition, there is a pseudo variable $this available. $this is a reference to the calling object. In fact, it refers to the instance of this class (pronoun)

3

The instance is also a specific object. To create an instance of a class, the new keyword must be used.

If new is followed by a string containing the class name, an instance of the class is created.

// 创建一个实例
<?php
$instance = new SimpleClass();

// 也可以这样做:
$className = &#39;Foo&#39;;
$instance = new $className(); // Foo()
?>
Copy after login

4 Class inheritance

can be understood as a child inheriting his father’s genes.

① A class can inherit the methods and properties of another class using the extends keyword in the declaration.

② PHP does not support multiple inheritance. A class can only inherit one base class (in fact, a child can only have one father).

③ Inherited methods and properties can be overridden by redeclaring them with the same name (it can be understood that the son has improved the father's original genes). But if the parent class uses final when defining a method, the method cannot be overridden. Overridden methods or properties can be accessed through parent::.

④ When overriding a method, the parameters must be consistent otherwise PHP will issue an E_STRICT level error message. The exception is constructors, which can take different parameters when overridden.

// 简单的类继承
<?php
class ExtendClass extends SimpleClass
{
    // 重新定义父类的方法
    function displayVar()
    {
        echo "继承类\n";
        parent::displayVar();
    }
}

$extended = new ExtendClass();
$extended->displayVar();
?>
Copy after login

5 Parsing of class names

Using ClassName::class you can get a string that contains the fully qualified name of the class ClassName.

// 类名的解析
<?php
namespace NS {
    class ClassName {
    }

    echo ClassName::class;
}
?>

// 以上输出结果: NS/ClassName
Copy after login

The above is the content of the PHP series (1). For more related content, please pay attention to the PHP Chinese website (m.sbmmt.com)!


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