Home  >  Article  >  Backend Development  >  34 PHP classes

34 PHP classes

WBOY
WBOYOriginal
2016-07-30 13:29:291158browse

1. Definition

<code>class 类名
{
    <span>...</span>
}</code>

2. Load class

<code><span>require</span><span>'./People.class.php'</span>;</code>

3. Automatically load class
In order to use a class, it is troublesome to include the class definition every time. Starting from PHP5, a __autoload() function is defined to implement automatic loading of classes. PHP will automatically call this function when using an undefined class.

<code><span><span>function</span><span>__autoload</span><span>(<span>$classname</span>)</span>
{</span><span>require_once</span><span>'./'</span> . <span>$classname</span> . <span>'.php'</span>;
}</code>

4. Class methods

<code>[ <span>static</span> | <span>final</span> ] 访问控制修饰符 <span><span>function</span> 方法名<span>(参数)</span>
{</span>
    ...
}
关键字 <span>static</span><span>final</span> 为可选项,
访问控制修饰符为<span>public</span>,<span>protected</span>,<span>private</span>中的一个。如果不指定,默认为<span>public</span>。
<span>static</span>关键字修饰的类方法为静态方法。在静态方法中,只能调用静态变量,不能调用普遍变量。
在普通方法中,可以调用静态变量。
在类的内部调用静态方法:<span>self</span>::静态方法。
在类的内部调用父类的静态方法:<span>parent</span>::静态方法。
如果在外部调用静态方法,则不用实例化,直接调用。
类名::静态方法</code>

5. Class attributes

<code>访问控制修饰符 [<span>static</span>] 属性名称;
访问控制修饰符为<span>public</span>,<span>protected</span>,<span>private</span>中的一个。默认为<span>public</span>。

如果要在类的方法中,访问属性:<span>$this</span>->属性名;

在类的内部访问静态属性:<span>self</span>::静态属性。
访问父类的静态属性:<span>parent</span>::静态属性。
注意,这里的静态属性名是要加 $ 美元符号的。
<span>self</span>::<span>$dollars</span> = <span>$dollars</span>;

在类外访问静态属性:  类名::属性名;  前提为可以访问,也是有 $ 符号。</code>

6. Class constants

<code>const <span>MALE</span> = <span>'男'</span>;    <span>//</span>常量名前面不加  <span>$ </span>。

类内访问:  <span>self:</span><span>:</span>常量名
          <span>parent:</span><span>:</span>常量名
不加 <span>$ </span> , <span>self:</span><span>:MALE</span>类外访问:  类名<span>:</span><span>:</span>常量名 ;   不加  <span>$ </span>。</code>

7. Constructors and destructors

<code><span>function</span> __construct()
{
    <span>...</span>  // 自动调用
}

<span>function</span> __destruct()
{
    <span>...</span>  //  无参
}</code>

8. Class inheritance

<code>class 子类名 extends 父类名
{
    <span>...</span>
}

如果子类中定义了构造方法,则子类在实例化时不会自动调用父类的构造方法。对于析构方法也是如此,
如果子类中定义了析构方法,则子类的实例在被销毁时不会自
动调用父类的析构方法。

显式调用父类的构造方法:
<span>function</span> __construct()
{
    <span>...</span>
    parent::__construct()
    {
        <span>...</span>
    }
    <span>...</span>
}

显式调用父类的析构方法:
<span>function</span> __destruct()
{
    <span>...</span>
    parent::__destruct()
    {
        <span>...</span>
    }
    <span>...</span>
}</code>

9. Polymorphism

Polymorphism is divided into static polymorphism and dynamic polymorphism

<code>静态多态:一个同名函数或者一个类中的同名方法,根据参数列表(类型以及个数)的不同来区别语义,
即所谓的函数重载。但PHP不支持函数重载。

动态多态:类的成员方法,能根据调用它的对象的类型的不同,自动做出适应性调整,
而且调整是发生在程序运行时的。PHP 中通过抽象类和接口
技术来实现动态多态性。</code>

Subclass overrides the parent class:
As long as a method with the same name as the parent class is defined in the subclass, it can be overridden. To access the parent class, use parent::method with the same name as the parent class.

If the parent class does not want its methods to be overridden by the subclass, use final to modify the parent class’s methods.


10. Abstract classes and abstract methods

An abstract class is a class that cannot be instantiated and can only be inherited by other classes as a parent class. An abstract class must contain an abstract method.
The so-called abstract method is a method without specific implementation, and its corresponding function body is empty. The details of abstract methods can only be implemented in subclasses, and subclasses must implement all abstract methods in the abstract class they inherit.

<code>abstract  class 抽象类名
{
    <span>...</span>
    abstract [public | protected ] <span>function</span> 抽象方法名(参数)
    {
        <span>...</span>
    }
}
其中,抽象方法的访问控制符只能为public,protected之一。如果抽象方法声明为public,则
子类中实现的方法也应声明为public;如果抽象方法声明为protected,
则子类中实现的方法既可以为protected,也可以声明为public。</code>

11. Interface

Whether it is a universal class or an abstract class, it can only implement single inheritance, that is, a subclass can only inherit one parent class. In fact, PHP only supports single inheritance. If you want to implement multiple inheritance, use interface technology to achieve it.

The difference between interfaces and classes:

<code>可以认为接口也是一种类,只是这种类中可以定义常量,但不能定义属性变量;可以定义方法,
但方法必须为空。也就是说,接口中只能定义常量和尚
未实现的方法,且方法必须为<span>public</span>(可以省略,
因为类的方法默认就是<span>public</span>)。

<span><span>interface</span> 接口名
{</span><span>const</span> 常量名 = 值;
    <span><span>function</span> 方法名<span>(参数)</span>
    {</span>
        ...
    }
}

实现:

<span><span>class</span> 子类名 <span>implements</span> 接口名1,接口名2 ..
{</span>
    ...
}</code>

Copyright statement: This article is an original article by the blogger and may not be reproduced without the blogger's permission.

The above has introduced the 34 PHP classes, including related content. I hope it will be helpful to friends who are interested in PHP tutorials.

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