作者:webabcd
介绍
速战速决 之 PHP
示例
1、类的相关知识点 1(基础)
class/class1.php
<span style="color: #000000;">php </span><span style="color: #008000;">/*</span><span style="color: #008000;">* * 类的相关知识点 1(基础) * * 规范:命名空间与目录路径对应,类名与文件名对应,文件以 .class.php 为后缀名 </span><span style="color: #008000;">*/</span> <span style="color: #0000ff;">class</span><span style="color: #000000;"> MyClass1 { </span><span style="color: #008000;">//</span><span style="color: #008000;"> 类常量,没有“$”符号,不能被覆盖</span> <span style="color: #0000ff;">const</span> MyConstant = 'constant value'<span style="color: #000000;">; </span><span style="color: #008000;">//</span><span style="color: #008000;"> 静态属性</span> <span style="color: #0000ff;">static</span> <span style="color: #800080;">$myStaticProperty</span> = "static property"<span style="color: #000000;">; </span><span style="color: #008000;">//</span><span style="color: #008000;"> 对于属性和方法的访问控制有 private protected public(默认值)</span> <span style="color: #0000ff;">private</span> <span style="color: #800080;">$_name</span><span style="color: #000000;">; </span><span style="color: #0000ff;">private</span> <span style="color: #800080;">$_age</span><span style="color: #000000;">; </span><span style="color: #008000;">//</span><span style="color: #008000;"> 构造函数 // PHP 中的魔术方法(magic method)均以 __(两个下划线)开头(类似的还有 __destruct(),__call(),__callStatic(),__get(),__set(),__isset(),__unset(),__sleep(),__wakeup(),__toString(),__invoke(),__set_state() 和 __clone() 等)</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> __construct() { </span><span style="color: #0000ff;">echo</span> "MyClass1 construct"<span style="color: #000000;">; </span><span style="color: #0000ff;">echo</span> "<br>"<span style="color: #000000;">; </span><span style="color: #008000;">//</span><span style="color: #008000;"> 获取参数个数</span> <span style="color: #800080;">$args_num</span> = <span style="color: #008080;">func_num_args</span><span style="color: #000000;">(); </span><span style="color: #0000ff;">if</span> (<span style="color: #800080;">$args_num</span> == 1<span style="color: #000000;">) { </span><span style="color: #008000;">//</span><span style="color: #008000;"> $this 代表当前对象,是指向类实例的指针</span> <span style="color: #800080;">$this</span>->_name = <span style="color: #008080;">func_get_arg</span>(0<span style="color: #000000;">); } </span><span style="color: #0000ff;">else</span> <span style="color: #0000ff;">if</span> (<span style="color: #800080;">$args_num</span> == 2<span style="color: #000000;">) { </span><span style="color: #800080;">$this</span>->_name = <span style="color: #008080;">func_get_arg</span>(0<span style="color: #000000;">); </span><span style="color: #800080;">$this</span>->_age = <span style="color: #008080;">func_get_arg</span>(1<span style="color: #000000;">); } </span><span style="color: #0000ff;">else</span><span style="color: #000000;"> { } } </span><span style="color: #008000;">//</span><span style="color: #008000;"> 析构函数</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> __destruct() { </span><span style="color: #0000ff;">print</span> "MyClass1 destruct"<span style="color: #000000;">; </span><span style="color: #0000ff;">echo</span> "<br>"<span style="color: #000000;">; } </span><span style="color: #008000;">//</span><span style="color: #008000;"> 构造函数,此种方式在 PHP 5.3.3 或以上可支持 // 此种方式的构造函数也可以当做方法被调用</span> <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> MyClass1() { </span><span style="color: #0000ff;">echo</span> "i am not a construct, i am a method"<span style="color: #000000;">; } </span><span style="color: #008000;">//</span><span style="color: #008000;"> 静态方法</span> <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> myStaticMethod() { </span><span style="color: #0000ff;">return</span> "static method"<span style="color: #000000;">; } </span><span style="color: #008000;">//</span><span style="color: #008000;"> 方法</span> <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> getInfo() { </span><span style="color: #008000;">//</span><span style="color: #008000;"> $this 代表当前对象,是指向类实例的指针</span> <span style="color: #0000ff;">return</span> "name: " . <span style="color: #800080;">$this</span>->_name . ", age: " . <span style="color: #800080;">$this</span>-><span style="color: #000000;">_age; } </span><span style="color: #008000;">//</span><span style="color: #008000;"> 不直接支持方法的重载(overload),可以通过相关的魔术方法来实现(参见:class3.php) // public function getInfo($name) { } // 带参数类型约束的方法,类型约束不能用于 int 或 string 之类的标量类型 // 本例约束了参数 $ary 必须是 array 类型</span> <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span> getFirst(<span style="color: #0000ff;">array</span> <span style="color: #800080;">$ary</span><span style="color: #000000;">) { </span><span style="color: #0000ff;">return</span> <span style="color: #800080;">$ary</span>[0<span style="color: #000000;">]; } } </span><span style="color: #008000;">//</span><span style="color: #008000;"> 被声明为 final 的类或属性或方法,无法继承 // 只能继承一个类</span> <span style="color: #0000ff;">final</span> <span style="color: #0000ff;">class</span> MyClass2 <span style="color: #0000ff;">extends</span><span style="color: #000000;"> MyClass1 { </span><span style="color: #008000;">//</span><span style="color: #008000;"> 构造函数可以为参数设置默认值(方法或函数也可以为参数设置默认值)</span> <span style="color: #0000ff;">function</span> __construct(<span style="color: #800080;">$name</span> = "wanglei", <span style="color: #800080;">$age</span> = 100<span style="color: #000000;">) { </span><span style="color: #0000ff;">echo</span> "MyClass2 construct"<span style="color: #000000;">; </span><span style="color: #0000ff;">echo</span> "<br>"<span style="color: #000000;">; </span><span style="color: #008000;">//</span><span style="color: #008000;"> parent 代表当前类的基类</span> parent::__construct(<span style="color: #800080;">$name</span>, <span style="color: #800080;">$age</span><span style="color: #000000;">); </span><span style="color: #008000;">//</span><span style="color: #008000;"> self 代表当前类 // $this 代表当前对象,是指向类实例的指针</span> <span style="color: #000000;"> } </span><span style="color: #008000;">//</span><span style="color: #008000;"> 析构函数</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> __destruct() { </span><span style="color: #0000ff;">print</span> "MyClass2 destruct"<span style="color: #000000;">; </span><span style="color: #0000ff;">echo</span> "<br>"<span style="color: #000000;">; parent</span>::<span style="color: #000000;">__destruct(); } </span><span style="color: #008000;">//</span><span style="color: #008000;"> 覆盖基类的同名方法(override)</span> <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> getInfo() { </span><span style="color: #008000;">//</span><span style="color: #008000;"> $this 代表当前对象,指向类实例的指针</span> <span style="color: #0000ff;">return</span> "MyClass2 - " . parent::<span style="color: #000000;">getInfo(); } } </span><span style="color: #008000;">//</span><span style="color: #008000;"> 类的实例化</span> <span style="color: #800080;">$objClass1</span> = <span style="color: #0000ff;">new</span> MyClass1("webabcd", 35<span style="color: #000000;">); </span><span style="color: #008000;">//</span><span style="color: #008000;"> 通过 -> 调用实例方法或实例属性</span> <span style="color: #0000ff;">echo</span> <span style="color: #800080;">$objClass1</span>-><span style="color: #000000;">getInfo(); </span><span style="color: #0000ff;">echo</span> "<br>"<span style="color: #000000;">; </span><span style="color: #008000;">//</span><span style="color: #008000;"> 通过 -> 调用实例方法或实例属性(MyClass1() 是构造函数,也可以当做方法被调用)</span> <span style="color: #0000ff;">echo</span> <span style="color: #800080;">$objClass1</span>-><span style="color: #000000;">MyClass1(); </span><span style="color: #0000ff;">echo</span> "<br>"<span style="color: #000000;">; </span><span style="color: #800080;">$objClass2</span> = <span style="color: #0000ff;">new</span><span style="color: #000000;"> MyClass2(); </span><span style="color: #0000ff;">echo</span> <span style="color: #800080;">$objClass2</span>-><span style="color: #000000;">getInfo(); </span><span style="color: #0000ff;">echo</span> "<br>"<span style="color: #000000;">; </span><span style="color: #008000;">//</span><span style="color: #008000;"> instanceof - 用于判断一个对象是否是指定类的实例</span> <span style="color: #0000ff;">if</span>(<span style="color: #800080;">$objClass2</span><span style="color: #000000;"> instanceof MyClass1) { </span><span style="color: #0000ff;">echo</span> '$objClass2 instanceof MyClass1'<span style="color: #000000;">; </span><span style="color: #0000ff;">echo</span> "<br>"<span style="color: #000000;">; } </span><span style="color: #008000;">//</span><span style="color: #008000;"> 通过 :: 调用类常量或静态属性或静态方法</span> <span style="color: #0000ff;">echo</span> MyClass1::<span style="color: #000000;">MyConstant; </span><span style="color: #0000ff;">echo</span> "<br>"<span style="color: #000000;">; </span><span style="color: #008000;">//</span><span style="color: #008000;"> 通过 :: 调用类常量或静态属性或静态方法</span> <span style="color: #0000ff;">echo</span> MyClass1::<span style="color: #800080;">$myStaticProperty</span><span style="color: #000000;">; </span><span style="color: #0000ff;">echo</span> "<br>"<span style="color: #000000;">; </span><span style="color: #008000;">//</span><span style="color: #008000;"> variable class(可变类),将变量的值作为类名</span> <span style="color: #800080;">$className</span> = 'MyClass1'<span style="color: #000000;">; </span><span style="color: #008000;">//</span><span style="color: #008000;"> variable method(可变方法),将变量的值作为方法名</span> <span style="color: #800080;">$methodName</span> = 'myStaticMethod'<span style="color: #000000;">; </span><span style="color: #008000;">//</span><span style="color: #008000;"> 通过 :: 调用类常量或静态属性或静态方法</span> <span style="color: #0000ff;">echo</span> <span style="color: #800080;">$className</span>::<span style="color: #800080;">$methodName</span><span style="color: #000000;">(); </span><span style="color: #0000ff;">echo</span> "<br>"<span style="color: #000000;">; </span><span style="color: #008000;">//</span><span style="color: #008000;"> 调用带参数类型约束的方法</span> <span style="color: #0000ff;">echo</span> <span style="color: #800080;">$objClass1</span>->getFirst(<span style="color: #0000ff;">array</span>("a", "b", "c"<span style="color: #000000;">)); </span><span style="color: #0000ff;">echo</span> "<br>";
2、类的相关知识点 2(抽象类,接口,trait)
class/class2.php
<span style="color: #000000;">php </span><span style="color: #008000;">/*</span><span style="color: #008000;">* * 类的相关知识点 2(抽象类,接口,trait) </span><span style="color: #008000;">*/</span> <span style="color: #008000;">//</span><span style="color: #008000;"> 抽象类</span> <span style="color: #0000ff;">abstract</span> <span style="color: #0000ff;">class</span><span style="color: #000000;"> MyAbstractClass { </span><span style="color: #008000;">//</span><span style="color: #008000;"> 抽象方法,子类必须定义这些方法</span> <span style="color: #0000ff;">abstract</span> <span style="color: #0000ff;">protected</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> getValue1(); </span><span style="color: #0000ff;">abstract</span> <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span> getValue2(<span style="color: #800080;">$param1</span><span style="color: #000000;">); </span><span style="color: #008000;">//</span><span style="color: #008000;"> 普通方法(非抽象方法)</span> <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> getValue0() { </span><span style="color: #0000ff;">return</span> "getValue0"<span style="color: #000000;">; } } </span><span style="color: #008000;">//</span><span style="color: #008000;"> 接口</span> <span style="color: #0000ff;">interface</span><span style="color: #000000;"> MyInterface1 { </span><span style="color: #008000;">//</span><span style="color: #008000;"> 接口常量,不能被覆盖</span> <span style="color: #0000ff;">const</span> MyConstant = 'constant value'<span style="color: #000000;">; </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> getValue3(); } </span><span style="color: #008000;">//</span><span style="color: #008000;"> 接口</span> <span style="color: #0000ff;">interface</span> MyInterface2 <span style="color: #0000ff;">extends</span><span style="color: #000000;"> MyInterface1 { </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> getValue4(); } </span><span style="color: #008000;">//</span><span style="color: #008000;"> 接口</span> <span style="color: #0000ff;">interface</span><span style="color: #000000;"> MyInterface3 { </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> getValue5(); } </span><span style="color: #008000;">//</span><span style="color: #008000;"> trait(可以 use 多个,允许有实现代码,但是本身不能实例化)</span> <span style="color: #000000;">trait MyTrait1 { </span><span style="color: #008000;">//</span><span style="color: #008000;"> 可以具有方法,静态方法,属性等</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> getValue6() { </span><span style="color: #0000ff;">return</span> "getValue6"<span style="color: #000000;">; } } </span><span style="color: #008000;">//</span><span style="color: #008000;"> trait(可以 use 多个,允许有实现代码,但是本身不能实例化)</span> <span style="color: #000000;">trait MyTrait2 { </span><span style="color: #008000;">//</span><span style="color: #008000;"> 抽象方法(use 这个 trait 的类必须要定义这个方法)</span> <span style="color: #0000ff;">abstract</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> getValue7(); } </span><span style="color: #008000;">//</span><span style="color: #008000;"> trait(可以 use 多个,允许有实现代码,但是本身不能实例化)</span> <span style="color: #000000;">trait MyTrait3 { </span><span style="color: #0000ff;">function</span><span style="color: #000000;"> getValue6() { </span><span style="color: #0000ff;">return</span> "getValue6"<span style="color: #000000;">; } </span><span style="color: #0000ff;">function</span><span style="color: #000000;"> getValue8() { </span><span style="color: #0000ff;">return</span> "getValue8"<span style="color: #000000;">; } } </span><span style="color: #008000;">//</span><span style="color: #008000;"> 必须实现所有抽象方法和接口方法 // 类只能单继承,接口可以多继承</span> <span style="color: #0000ff;">class</span> MyClass1 <span style="color: #0000ff;">extends</span> MyAbstractClass <span style="color: #0000ff;">implements</span> MyInterface2,<span style="color: #000000;"> MyInterface3 { </span><span style="color: #008000;">//</span><span style="color: #008000;"> 可以 use 多个 trait</span> <span style="color: #0000ff;">use</span> MyTrait1,<span style="color: #000000;"> MyTrait2; </span><span style="color: #0000ff;">use</span><span style="color: #000000;"> MyTrait3 { </span><span style="color: #008000;">//</span><span style="color: #008000;"> 多 trait 间有重名的,可以指定以哪个为准</span> MyTrait1::<span style="color: #000000;">getValue6 insteadof MyTrait3; </span><span style="color: #008000;">//</span><span style="color: #008000;"> 可以为 trait 的指定方法设置别名(调用的时候用方法名也行,用别名也行)</span> MyTrait3::getValue8 <span style="color: #0000ff;">as</span><span style="color: #000000;"> alias; } </span><span style="color: #008000;">//</span><span style="color: #008000;"> 可以将 protected 升级为 public</span> <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> getValue1() { </span><span style="color: #0000ff;">return</span> "getValue1"<span style="color: #000000;">; } </span><span style="color: #008000;">//</span><span style="color: #008000;"> 可以加参数,但是加的参数必须要有默认值</span> <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span> getValue2(<span style="color: #800080;">$param1</span>, <span style="color: #800080;">$param2</span> = 'param2'<span style="color: #000000;">) { </span><span style="color: #0000ff;">return</span> "getValue2, {<span style="color: #800080;">$param1</span>}, {<span style="color: #800080;">$param2</span>}"<span style="color: #000000;">; } </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> getValue3() { </span><span style="color: #0000ff;">return</span> "getValue3"<span style="color: #000000;">; } </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> getValue4() { </span><span style="color: #0000ff;">return</span> "getValue4"<span style="color: #000000;">; } </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> getValue5() { </span><span style="color: #0000ff;">return</span> "getValue5"<span style="color: #000000;">; } </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> getValue7() { </span><span style="color: #0000ff;">return</span> "getValue7"<span style="color: #000000;">; } } </span><span style="color: #008000;">//</span><span style="color: #008000;"> 调用接口常量</span> <span style="color: #0000ff;">echo</span> MyInterface1::<span style="color: #000000;">MyConstant; </span><span style="color: #0000ff;">echo</span> "<br>"<span style="color: #000000;">; </span><span style="color: #800080;">$myClass1</span> = <span style="color: #0000ff;">new</span><span style="color: #000000;"> MyClass1; </span><span style="color: #0000ff;">echo</span> <span style="color: #800080;">$myClass1</span>-><span style="color: #000000;">getValue0(); </span><span style="color: #0000ff;">echo</span> "<br>"<span style="color: #000000;">; </span><span style="color: #0000ff;">echo</span> <span style="color: #800080;">$myClass1</span>-><span style="color: #000000;">getValue1(); </span><span style="color: #0000ff;">echo</span> "<br>"<span style="color: #000000;">; </span><span style="color: #0000ff;">echo</span> <span style="color: #800080;">$myClass1</span>->getValue2("webabcd"<span style="color: #000000;">); </span><span style="color: #0000ff;">echo</span> "<br>"<span style="color: #000000;">; </span><span style="color: #0000ff;">echo</span> <span style="color: #800080;">$myClass1</span>-><span style="color: #000000;">getValue3(); </span><span style="color: #0000ff;">echo</span> "<br>"<span style="color: #000000;">; </span><span style="color: #0000ff;">echo</span> <span style="color: #800080;">$myClass1</span>-><span style="color: #000000;">getValue4(); </span><span style="color: #0000ff;">echo</span> "<br>"<span style="color: #000000;">; </span><span style="color: #0000ff;">echo</span> <span style="color: #800080;">$myClass1</span>-><span style="color: #000000;">getValue5(); </span><span style="color: #0000ff;">echo</span> "<br>"<span style="color: #000000;">; </span><span style="color: #0000ff;">echo</span> <span style="color: #800080;">$myClass1</span>-><span style="color: #000000;">getValue6(); </span><span style="color: #0000ff;">echo</span> "<br>"<span style="color: #000000;">; </span><span style="color: #0000ff;">echo</span> <span style="color: #800080;">$myClass1</span>-><span style="color: #000000;">getValue7(); </span><span style="color: #0000ff;">echo</span> "<br>"<span style="color: #000000;">; </span><span style="color: #0000ff;">echo</span> <span style="color: #800080;">$myClass1</span>-><span style="color: #000000;">getValue8(); </span><span style="color: #0000ff;">echo</span> "<br>"<span style="color: #000000;">; </span><span style="color: #0000ff;">echo</span> <span style="color: #800080;">$myClass1</span>-><span style="color: #000000;">alias(); </span><span style="color: #0000ff;">echo</span> "<br>";
OK
[源码下载]