PHP 實(shí)現(xiàn)代碼復(fù)用的一個方法 traits新特性(2)_PHP教程
推薦:php數(shù)組鍵名技巧小結(jié)這篇文章主要介紹了php數(shù)組鍵名技巧小結(jié),分析了php數(shù)組鍵名常見的使用技巧,具有一定參考借鑒價值,需要的朋友可以參考下 本文較為詳細(xì)的總結(jié)了php數(shù)組鍵名的技巧。分享給大家供大家參考。具體分析如下: 1、$arr[true] 等價于 $arr[1];$arr[false] 等價于 $arr[0]。 2
代碼如下:
<?php
trait Hello {
public function sayHelloWorld() {
echo 'Hello'.$this->getWorld();
}
abstract public function getWorld();
}
class MyHelloWorld {
private $world;
use Hello;
public function getWorld() {
return $this->world;
}
public function setWorld($val) {
$this->world = $val;
}
}
?>
Trait 的靜態(tài)成員
Traits 可以被靜態(tài)成員靜態(tài)方法定義。
靜態(tài)變量的例子
代碼如下:
<?php
trait Counter {
public function inc() {
static $c = 0;
$c = $c + 1;
echo "$c\n";
}
}
class C1 {
use Counter;
}
class C2 {
use Counter;
}
$o = new C1(); $o->inc(); // echo 1
$p = new C2(); $p->inc(); // echo 1
?>
靜態(tài)方法的例子
代碼如下:
<?php
trait StaticExample {
public static function doSomething() {
return 'Doing something';
}
}
class Example {
use StaticExample;
}
Example::doSomething();
?>
靜態(tài)變量和靜態(tài)方法的例子
代碼如下:
<?php
trait Counter {
public static $c = 0;
public static function inc() {
self::$c = self::$c + 1;
echo self::$c . "\n";
}
}
class C1 {
use Counter;
}
class C2 {
use Counter;
}
C1::inc(); // echo 1
C2::inc(); // echo 1
?>
屬性
Trait 同樣可以定義屬性。
定義屬性的例子
代碼如下:
<?php
trait PropertiesTrait {
public $x = 1;
}
class PropertiesExample {
use PropertiesTrait;
}
$example = new PropertiesExample;
$example->x;
?>
如果 trait 定義了一個屬性,那類將不能定義同樣名稱的屬性,否則會產(chǎn)生一個錯誤。如果該屬性在類中的定義與在 trait 中的定義兼容(同樣的可見性和初始值)則錯誤的級別是 E_STRICT,否則是一個致命錯誤。
沖突的例子
代碼如下:
<?php
trait PropertiesTrait {
public $same = true;
public $different = false;
}
class PropertiesExample {
use PropertiesTrait;
public $same = true; // Strict Standards
public $different = true; // 致命錯誤
}
?>
Use的不同
不同use的例子
代碼如下:
<?php
namespace Foo\Bar;
use Foo\Test; // means \Foo\Test - the initial \ is optional
?>
<?php
namespace Foo\Bar;
class SomeClass {
use Foo\Test; // means \Foo\Bar\Foo\Test
}
?>
第一個use是用于 namespace 的 use Foo\Test,找到的是 \Foo\Test,第二個 use 是使用一個trait,找到的是\Foo\Bar\Foo\Test。
__CLASS__和__TRAIT__
__CLASS__ 返回 use trait 的 class name,__TRAIT__返回 trait name
示例如下
代碼如下:
<?php
trait TestTrait {
public function testMethod() {
echo "Class: " . __CLASS__ . PHP_EOL;
echo "Trait: " . __TRAIT__ . PHP_EOL;
}
}
class BaseClass {
use TestTrait;
}
class TestClass extends BaseClass {
}
$t = new TestClass();
$t->testMethod();
//Class: BaseClass
//Trait: TestTrait
Trait單例
實(shí)例如下
代碼如下:
<?php
trait singleton {
/**
* private construct, generally defined by using class
*/
//private function __construct() {}
public static function getInstance() {
static $_instance = NULL;
$class = __CLASS__;
return $_instance ?: $_instance = new $class;
}
public function __clone() {
trigger_error('Cloning '.__CLASS__.' is not allowed.',E_USER_ERROR);
}
public function __wakeup() {
trigger_error('Unserializing '.__CLASS__.' is not allowed.',E_USER_ERROR);
}
}
/**
* Example Usage
*/
class foo {
use singleton;
private function __construct() {
$this->name = 'foo';
}
}
class bar {
use singleton;
private function __construct() {
$this->name = 'bar';
}
}
$foo = foo::getInstance();
echo $foo->name;
$bar = bar::getInstance();
echo $bar->name;
調(diào)用trait方法
雖然不很明顯,但是如果Trait的方法可以被定義為在普通類的靜態(tài)方法,就可以被調(diào)用
實(shí)例如下
代碼如下:<?php
trait Foo {
function bar() {
return 'baz';
}
}
echo Foo::bar(),"\\n";
?>
小伙伴們對于traits的新特性是否熟悉了呢,希望本文能對大家有所幫助。
分享:php使用explode()函數(shù)將字符串拆分成數(shù)組的方法這篇文章主要介紹了php使用explode()函數(shù)將字符串拆分成數(shù)組的方法,具有一定參考借鑒價值,需要的朋友可以參考下 本文實(shí)例講述了php使用explode()函數(shù)將字符串拆分成數(shù)組的方法。分享給大家供大家參考。具體分析如下: explode()函數(shù):字符串拆分成數(shù)組 示例代碼如下:
- php數(shù)組鍵名技巧小結(jié)
- php使用explode()函數(shù)將字符串拆分成數(shù)組的方法
- php選擇排序法實(shí)現(xiàn)數(shù)組排序?qū)嵗治?/a>
- php插入排序法實(shí)現(xiàn)數(shù)組排序?qū)嵗?/a>
- php數(shù)組添加與刪除單元的常用函數(shù)實(shí)例分析
- Yii學(xué)習(xí)總結(jié)之安裝配置
- 使用php的HTTP請求的庫Requests實(shí)現(xiàn)美女圖片墻
- PHP實(shí)現(xiàn)加密的幾種方式介紹
- php使用parse_url和parse_str解析URL
- 在Windows XP下安裝Apache+MySQL+PHP環(huán)境
- PHP+APACHE實(shí)現(xiàn)網(wǎng)址偽靜態(tài)
- php使用unset()刪除數(shù)組中某個單元(鍵)的方法
- 相關(guān)鏈接:
- 教程說明:
PHP教程-PHP 實(shí)現(xiàn)代碼復(fù)用的一個方法 traits新特性(2)
。