一个PHP的动态泛型。我做的。下面是测试及使用的例子<?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不是TObjectclass 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>

解决方案 »

  1.   


    TChild::PrepareGeneric()
    initialize ...
    $this->GenericArg()
    object construct...
      

  2.   

    不是这样的
    是:TChild::PrepareGeneric(array ('T' => integer));
    $Obj = new TChild();
    如果不需要泛型就是直接new,不要PrepareGeneric...我说的是类型安全  本身我的泛型也是动态的。。那么有没有必要把如何实现的代码贴出来呢??
      

  3.   

    php里面有对象集合这么一说吗?就比如.net里面的LIst<T>泛型??