类与对象(PHP5)

了内省一个属性,你必须首先创建ReflectionProperty类的一个实例,你可以随后访问这个实例的任何上述方法。
例子 19-37. Using the ReflectionProperty class复制PHP内容到剪贴板PHP代码:

<?php
class String
{   public $length  = 5;   }
// Create an instance of the ReflectionProperty class
$prop=new ReflectionProperty('String','length');
// Print out basic information
printf( "===> The%s%s%s%s property '%s' (which was %s)\n" .
    "   having the modifiers %s\n",
        $prop->isPublic() ? ' public' : '',
        $prop->isPrivate() ? ' private' : '',
        $prop->isProtected() ? ' protected' : '',
        $prop->isStatic() ? ' static' : '',
        $prop->getName(),
        $prop->isDefault() ? 'declared at compile-time' : 'created at run-time',
        var_export(Reflection::getModifierNames($prop->getModifiers()),1)
);
$obj= new String();//Create an instance of String
printf("--->Value is: ");//Get current value
var_dump($prop->getValue($obj));
$prop->setValue($obj,10); // Change value
printf("---> Setting value to 10, new value is: ");
var_dump($prop->getValue($obj));
var_dump($obj); //Dump object
?>

注:试图获得或设置私有的或保护的类属性的值将导致一个被抛出的异常。
映射扩展(ReflectionExtension)
ReflectionExtension类允许你反向设计扩展。你可以在使用get_loaded_extensions()运行的时候重新返回所有加载的扩展。复制PHP内容到剪贴板PHP代码:

<?php
class ReflectionExtension implements Reflector
{   final private __clone()
    public __construct(string name)
    public string __toString()
    public static string export()
    public string getName()
    public string getVersion()
    public ReflectionFunction[] getFunctions()
    public array getConstants()
    public array getINIEntries()
    public ReflectionClass[] getClasses()
    public array getClassNames()
}
?>

为了内省一个扩展,你必须首先创建ReflectionExtension类的一个实例。你可以随后访问这个实例的任何上述方法。
例子 19-38. 使用ReflectionExtension 的类复制PHP内容到剪贴板PHP代码:

<?php
// Create an instance of the ReflectionProperty class
$ext=new ReflectionExtension('standard');
// Print out basic information
printf("Name        : %s\n" .
    "Version     : %s\n" .
    "Functions   : [%d] %s\n" .
    "Constants   : [%d] %s\n" .
    "INI entries : [%d] %s\n" .
    "Classes     : [%d] %s\n",
        $ext->getName(),
        $ext->getVersion()?$ext->getVersion():'NO_VERSION',
        sizeof($ext->getFunctions()),
        var_export($ext->getFunctions(),1),
        sizeof($ext->getConstants()),
        var_export($ext->getConstants(),1),
        sizeof($ext->getINIEntries()),
        var_export($ext->getINIEntries(),1),
        sizeof($ext->getClassNames()),
        var_export($ext->getClassNames(),1)
);
?>

扩展中的映射类(Extending the reflection classes)
万一你想创建一个构建类的专用的形式(就是说:在输出时创建彩色的HTML,拥有易访问的成员变量来代替方法或拥有公共方法),你可以继续和扩展它们。
例子 19-39. Extending the built-in classes复制PHP内容到剪贴板PHP代码:

<?php
/**  My Reflection_Method class  */
class My_Reflection_Method extends ReflectionMethod
{   public $visibility = '';
    public function __construct($o, $m)
    {   parent::__construct($o, $m);
        $this->visibility= Reflection::getModifierNames($this->getModifiers());
    }
}
class T {  protected function x() { }  } //Demo class #1
class U extends T {  function x() { }  } //Demo class #2
var_dump(new My_Reflection_Method('U','x'));//Print out information
?>

注意警告:如果你重写构造函数,请记住在你插入任何代码之前通知父类的构造函数。不这样做将导致以下:致命错误: 内部错误:返回映射对象失败。


第19章 类与对象(PHP5)之十五:典型提示(线索)Type Hinting


PHP5引入了类型提示。函数现在能给对象强制参数(在函数原型中通过指定类的名字)或数组(从PHP 5.1开始) 。
例子 19-40. Type Hinting examples复制PHP内容到剪贴板PHP代码:

<?php
class MyClass // An example class
{/**A test function
    * First parameter must be an object of type OtherClass */
    public function test(OtherClass $otherclass) { echo $otherclass->var; }
    /*Another test function  * First parameter must be an array*/
    public function test_array(array $input_array){  print_r($input_array); }
}
class OtherClass{  public $var='Hello World';  }//Another example class
?>

Failing to satisfy the type hint results in a fatal error.复制PHP内容到剪贴板PHP代码:

<?php
// An instance of each class
$myclass = new MyClass;
$otherclass = new OtherClass;
// Fatal Error: Argument 1 must be an object of class OtherClass
$myclass->test('hello');
// Fatal Error: Argument 1 must be an instance of OtherClass
$foo = new stdClass;
$myclass->test($foo);
$myclass->test(null); //Fatal Error: Argument 1 must not be null
$myclass->test($otherclass); //Works: Prints Hello World
$myclass->test_array('a string');//Fatal Error:Argument 1 must be an array
$myclass->test_array(array('a', 'b', 'c'));//Works: Prints the array
?>

Type hinting also works with functions:
类型提示也可以与函数协同工作。复制PHP内容到剪贴板PHP代码:

<?php
class MyClass {  public $var = 'Hello World';  }//An example class
/**A test function * First parameter must be an object of type MyClass  */
function MyFunction(MyClass $foo){  echo $foo->var;  }
$myclass = new MyClass; //Works
MyFunction($myclass);
?>

类型提示可以只是对象和数组(从PHP 5.1开始)类型。传统的类型提示不支持整型和字符串型。

在此我把“Type Hinting”翻译为“类型提示”不知道合适不合适?
请大家提出建议,谢谢!!

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值