Home  >  Article  >  Backend Development  >  Quick Fix 4 - PHP: Class Basics, Abstract Classes, Interfaces, Traits

Quick Fix 4 - PHP: Class Basics, Abstract Classes, Interfaces, Traits

WBOY
WBOYOriginal
2016-07-29 08:51:101160browse

[Source code download]

Quick Solution (4) - PHP: Class Basics, Abstract Classes, Interfaces, Traits


Author: webabcd
Introduction
Quick Solution to PHP

  • Class Basics
  • Abstract Class
  • Interface
  • trait


Example
1. Class-related knowledge point 1 (basic)
class/class1.php

php
/**
 * 类的相关知识点 1(基础)
 *
 * 规范:命名空间与目录路径对应,类名与文件名对应,文件以 .class.php 为后缀名
 */class MyClass1
{
    // 类常量,没有“$”符号,不能被覆盖const MyConstant = 'constant value';
    // 静态属性static$myStaticProperty = "static property";

    // 对于属性和方法的访问控制有 private protected public(默认值)private$_name;
    private$_age;

    // 构造函数
    // PHP 中的魔术方法(magic method)均以 __(两个下划线)开头(类似的还有 __destruct(),__call(),__callStatic(),__get(),__set(),__isset(),__unset(),__sleep(),__wakeup(),__toString(),__invoke(),__set_state() 和 __clone() 等)function __construct()
    {
        echo "MyClass1 construct";
        echo "
"; // 获取参数个数$args_num = func_num_args(); if ($args_num == 1) { // $this 代表当前对象,是指向类实例的指针$this->_name = func_get_arg(0); } elseif ($args_num == 2) { $this->_name = func_get_arg(0); $this->_age = func_get_arg(1); } else { } } // 析构函数function __destruct() { print "MyClass1 destruct"; echo "
"; } // 构造函数,此种方式在 PHP 5.3.3 或以上可支持 // 此种方式的构造函数也可以当做方法被调用publicfunction MyClass1() { echo "i am not a construct, i am a method"; } // 静态方法publicstaticfunction myStaticMethod() { return "static method"; } // 方法publicfunction getInfo() { // $this 代表当前对象,是指向类实例的指针return "name: " . $this->_name . ", age: " . $this->_age; } // 不直接支持方法的重载(overload),可以通过相关的魔术方法来实现(参见:class3.php) // public function getInfo($name) { } // 带参数类型约束的方法,类型约束不能用于 int 或 string 之类的标量类型 // 本例约束了参数 $ary 必须是 array 类型publicfunction getFirst(array$ary) { return$ary[0]; } } // 被声明为 final 的类或属性或方法,无法继承 // 只能继承一个类finalclass MyClass2 extends MyClass1 { // 构造函数可以为参数设置默认值(方法或函数也可以为参数设置默认值)function __construct($name = "wanglei", $age = 100) { echo "MyClass2 construct"; echo "
"; // parent 代表当前类的基类 parent::__construct($name, $age); // self 代表当前类 // $this 代表当前对象,是指向类实例的指针 } // 析构函数function __destruct() { print "MyClass2 destruct"; echo "
"; parent::__destruct(); } // 覆盖基类的同名方法(override)publicfunction getInfo() { // $this 代表当前对象,指向类实例的指针return "MyClass2 - " . parent::getInfo(); } } // 类的实例化$objClass1 = new MyClass1("webabcd", 35); // 通过 -> 调用实例方法或实例属性echo$objClass1->getInfo(); echo "
"; // 通过 -> 调用实例方法或实例属性(MyClass1() 是构造函数,也可以当做方法被调用)echo$objClass1->MyClass1(); echo "
"; $objClass2 = new MyClass2(); echo$objClass2->getInfo(); echo "
"; // instanceof - 用于判断一个对象是否是指定类的实例if($objClass2 instanceof MyClass1) { echo '$objClass2 instanceof MyClass1'; echo "
"; } // 通过 :: 调用类常量或静态属性或静态方法echo MyClass1::MyConstant; echo "
"; // 通过 :: 调用类常量或静态属性或静态方法echo MyClass1::$myStaticProperty; echo "
"; // variable class(可变类),将变量的值作为类名$className = 'MyClass1'; // variable method(可变方法),将变量的值作为方法名$methodName = 'myStaticMethod'; // 通过 :: 调用类常量或静态属性或静态方法echo$className::$methodName(); echo "
"; // 调用带参数类型约束的方法echo$objClass1->getFirst(array("a", "b", "c")); echo "
";


2. Class-related knowledge point 2 (abstract class, interface, trait)
class/class2.php

php
/**
 * 类的相关知识点 2(抽象类,接口,trait)
 */// 抽象类abstractclass MyAbstractClass
{
    // 抽象方法,子类必须定义这些方法abstractprotectedfunction getValue1();
    abstractpublicfunction getValue2($param1);

    // 普通方法(非抽象方法)publicfunction getValue0()
    {
        return "getValue0";
    }
}

// 接口interface MyInterface1
{
    // 接口常量,不能被覆盖const MyConstant = 'constant value';
    publicfunction getValue3();
}

// 接口interface MyInterface2 extends MyInterface1
{
    publicfunction getValue4();
}

// 接口interface MyInterface3
{
    publicfunction getValue5();
}

// trait(可以 use 多个,允许有实现代码,但是本身不能实例化)trait MyTrait1
{
    // 可以具有方法,静态方法,属性等function getValue6()
    {
        return "getValue6";
    }
}

// trait(可以 use 多个,允许有实现代码,但是本身不能实例化)trait MyTrait2
{
    // 抽象方法(use 这个 trait 的类必须要定义这个方法)abstractfunction getValue7();
}

// trait(可以 use 多个,允许有实现代码,但是本身不能实例化)trait MyTrait3
{
    function getValue6()
    {
        return "getValue6";
    }

    function getValue8()
    {
        return "getValue8";
    }
}

// 必须实现所有抽象方法和接口方法
// 类只能单继承,接口可以多继承class MyClass1 extends MyAbstractClass implements MyInterface2, MyInterface3
{
    // 可以 use 多个 traituse MyTrait1, MyTrait2;
    use MyTrait3
    {
        // 多 trait 间有重名的,可以指定以哪个为准        MyTrait1::getValue6 insteadof MyTrait3;
        // 可以为 trait 的指定方法设置别名(调用的时候用方法名也行,用别名也行)        MyTrait3::getValue8 as alias;
    }

    // 可以将 protected 升级为 publicpublicfunction getValue1()
    {
        return "getValue1";
    }

    // 可以加参数,但是加的参数必须要有默认值publicfunction getValue2($param1, $param2 = 'param2')
    {
        return "getValue2, {$param1}, {$param2}";
    }

    publicfunction getValue3()
    {
        return "getValue3";
    }

    publicfunction getValue4()
    {
        return "getValue4";
    }

    publicfunction getValue5()
    {
        return "getValue5";
    }

    publicfunction getValue7()
    {
        return "getValue7";
    }
}

// 调用接口常量echo MyInterface1::MyConstant;
echo "
"; $myClass1 = new MyClass1; echo$myClass1->getValue0(); echo "
"; echo$myClass1->getValue1(); echo "
"; echo$myClass1->getValue2("webabcd"); echo "
"; echo$myClass1->getValue3(); echo "
"; echo$myClass1->getValue4(); echo "
"; echo$myClass1->getValue5(); echo "
"; echo$myClass1->getValue6(); echo "
"; echo$myClass1->getValue7(); echo "
"; echo$myClass1->getValue8(); echo "
"; echo$myClass1->alias(); echo "
";


OK
[Source code download]

The above has introduced Quick Solution 4 - PHP: Class Basics, Abstract Classes, Interfaces, Traits, including aspects of the 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