21,886
社区成员
发帖
与我相关
我的任务
分享
<?php
include 'FrameworkDSW/System.php';
class TChild extends TObject {
/**
* @param T $p
*/
public function Func($p) {
TType::Type($p, $this->GenericArg('T'));
}
/**
* @param T $t
* @param P $p
*/
public function FuncTwo($t, $p) {
TType::Type($t, $this->GenericArg('T'));
TType::Type($p, $this->GenericArg('P'));
}
}
TChild::PrepareGeneric(array ('T' => 'integer'));
$obj = new TChild();
$obj->Func(0);
$obj->Func(new TObject()); //此处出错!不是integer
$obj->Func(true); //不出错,因为true是可以转换成1的,根据PHP的==号规则
$obj->Func('string'); //不出错,因为可以转换成0,根据PHP的==号规则
TChild::PrepareGeneric(array ('T' => 'integer', 'P' => 'TObject'));
$obj = new TChild();
$obj->FuncTwo(0, new TObject());
$obj->FuncTwo(new TObject(), 0); //出错,TObject不是integer,0不是TObject
class TComplex extends TObject {
/**
* @param T $t
*/
public function Func($t) {
TType::Type($t, $this->GenericArg('T'));
}
}
TChild::PrepareGeneric(array ('T' => 'integer', 'P' => 'boolean'));
$c = new TChild();
TComplex::PrepareGeneric(array ('T' => array ('TChild' => array ('T' => 'integer', 'P' => 'boolean'))));
$obj = new TComplex();
$obj->Func($c);
$obj->Func(true);//出错:true不是TChild<T: integer, P: boolean>
TChild::PrepareGeneric(array ('T' => integer));
$Obj = new TChild();
如果不需要泛型就是直接new,不要PrepareGeneric...