源代码:
$reflection_class = new ReflectionClass('Controllercommonfooter');
var_dump($reflection_class); #output_1
$reflection_method = $reflection_class->getMethod('index');
var_dump($reflection_method); #output_2
var_dump($reflection_method->isProtected()); #output_3
输出结果是:
object(ReflectionClass)[36]
public 'name' => string 'ControllerCommonFooter' (length=22)object(ReflectionMethod)[40]
public 'name' => string 'index' (length=5)
public 'class' => string 'ControllerCommonFooter' (length=22)boolean true
我通过var_dump($reflection_method->isProtected()); 确定类Controllercommonfooter的方法index的访问性是protected。
但
但
奇怪的是,下面的代码正常运行了,我搞不明白:
$file = '指向Controllercommonfooter类的文件路径';
$class = 'Controllercommonfooter';
$method = 'index';
$args = array();
//run
$controller = new $class();
$controller->$method($args); #正常运行了,有返回结果。我想请大家解释一下设置为protected 的类方法在什么情况下可以这样直接调用?????
我想请大家解释一下设置为protected 的类方法在什么情况下可以这样直接调用?????
我想请大家解释一下设置为protected 的类方法在什么情况下可以这样直接调用?????
我想请大家解释一下设置为protected 的类方法在什么情况下可以这样直接调用?????
我想请大家解释一下设置为protected 的类方法在什么情况下可以这样直接调用?????
我想请大家解释一下设置为protected 的类方法在什么情况下可以这样直接调用?????
谢谢!
谢谢!
谢谢!
万分谢谢!!!
$reflection_class = new ReflectionClass('Controllercommonfooter');
var_dump($reflection_class); #output_1
$reflection_method = $reflection_class->getMethod('index');
var_dump($reflection_method); #output_2
var_dump($reflection_method->isProtected()); #output_3
输出结果是:
object(ReflectionClass)[36]
public 'name' => string 'ControllerCommonFooter' (length=22)object(ReflectionMethod)[40]
public 'name' => string 'index' (length=5)
public 'class' => string 'ControllerCommonFooter' (length=22)boolean true
我通过var_dump($reflection_method->isProtected()); 确定类Controllercommonfooter的方法index的访问性是protected。
但
但
奇怪的是,下面的代码正常运行了,我搞不明白:
$file = '指向Controllercommonfooter类的文件路径';
$class = 'Controllercommonfooter';
$method = 'index';
$args = array();
//run
$controller = new $class();
$controller->$method($args); #正常运行了,有返回结果。我想请大家解释一下设置为protected 的类方法在什么情况下可以这样直接调用?????
我想请大家解释一下设置为protected 的类方法在什么情况下可以这样直接调用?????
我想请大家解释一下设置为protected 的类方法在什么情况下可以这样直接调用?????
我想请大家解释一下设置为protected 的类方法在什么情况下可以这样直接调用?????
我想请大家解释一下设置为protected 的类方法在什么情况下可以这样直接调用?????
我想请大家解释一下设置为protected 的类方法在什么情况下可以这样直接调用?????
谢谢!
谢谢!
谢谢!
万分谢谢!!!
解决方案 »
- 调用date("1")函数,页面显示500 - Internal Server Error是怎么回事
- 求助怎么带入变量呢啊?
- 从数据库取出图片的路径,怎么给<input type="file" name="imgurl" value="{$coupons_url}">赋值
- php怎么显示xml内容出来
- 关于启用seesion后页面的图片不刷新的问题
- PHP环境配置问题,急!解决就给分
- 使用linux as3自带的php如何连接oracle9i数据库?请解答
- 请问如何把用cookie的bbs改为用session??
- 模拟登陆 需要输入验证码
- PHP正则大空格“ ”匹配中文“一”?
- 帮我解决个小问题,谢谢
- 求教SWFUpload上传的结果如何返回显示
protected function index() {
echo 'abcd';
}
}$reflection_class = new ReflectionClass('Controllercommonfooter');
var_dump($reflection_class); #output_1
$reflection_method = $reflection_class->getMethod('index');
var_dump($reflection_method); #output_2
var_dump($reflection_method->isProtected()); #output_3$class = 'Controllercommonfooter';
$method = 'index';
$args = array();$controller = new $class();
$controller->$method($args); Fatal error: Call to protected method Controllercommonfooter::index() from context '' in ...
TRUE if the method is protected, otherwise FALSE
首先确定这个返回的是TRUE
其次看清有无子类重载此方法
protected只了类内或间调用,非外部调用
想调用,一是子类重载此方法
二是写一个方法取得内容,就像__GET与__SET差不多
protected test(){echo "TEST";}
public getTest(){$this->test();}
可以外部直接调用的我还未见过,这不就违反了protected的初衷了嘛
class Action{
private $class;
private $method;
public function __construct($route)
{
list($this->class, $this->method) = explode('/', $route);
}
public function getClass()
{
return $this->class;
}
public function getMethod()
{
return $this->method;
}
}class Controller
{
protected $children = array();
protected $output;
public function __construct() {}
protected function getChild($child) {
$action = new Action($child);
$class = $action->getClass();
$method = $action->getMethod();
$controller = new $class(); /*********注意这里*********/
$controller->$method(); /*********注意这里*********/
return $controller->output;
}
public function render() {
foreach ($this->children as $child) {
$this->data[basename($child)] = $this->getChild($child);
}
$this->output .= implode('<br />------------------<br />', $this->data);
//echo $this->data[basename($child)];
return $this->output;
}
public function output()
{
return $this->output;
}
}class Kcontroller extends Controller
{
public function index()
{
$this->children = array('footer/footer', 'header/header');
}
}class Footer extends Controller
{
protected function footer()
{
$this->output = 'This is Page Footer!!!';
}
}class Header extends Controller
{
protected function header()
{
$this->output = 'This is Page Header!!!';
}
}
$k = new Kcontroller();
$k->index();
$k->render();
echo $k->output();
你把这个function output改为protected立马报错
这就是protedted保护的意思,只能在类内调用不可外部调用
private function ppt()
{
echo 'blah blah';
}
然后在Controller类的getChild方法中添加:
$controller->ppt();目的是为了显示当程序执行到哪里时的上下文环境,结果显示的是:
Fatal error: Call to private method Footer::ppt() from context 'Controller'
PHP手册里有这么一段:
Members declared protected can be accessed only within the class itself and by inherited and parent classes.难道指的是这个。
Members declared protected can be accessed within the parent classes。代码如下:
<?php
class Action{
private $class;
private $method;
public function __construct($route)
{
list($this->class, $this->method) = explode('/', $route);
}
public function getClass()
{
return $this->class;
}
public function getMethod()
{
return $this->method;
}
}class Controller
{
protected $children = array();
protected $output;
public function __construct() {}
protected function getChild($child) {
$action = new Action($child);
$class = $action->getClass();
$method = $action->getMethod();
$controller = new $class(); /*********注意这里*********/
$controller->$method(); /*********注意这里*********/
$controller->ppt();
return $controller->output;
}
public function render() {
foreach ($this->children as $child) {
$this->data[basename($child)] = $this->getChild($child);
}
$this->output .= implode('<br />------------------<br />', $this->data);
//echo $this->data[basename($child)];
return $this->output;
}
public function output()
{
return $this->output;
}
}class Kcontroller extends Controller
{
public function index()
{
$this->children = array('footer/footer', 'header/header');
}
}class Footer extends Controller
{
protected function footer()
{
$this->output = 'This is Page Footer!!!';
}
private function ppt()
{
echo 'blah blah';
}
}class Header extends Controller
{
protected function header()
{
$this->output = 'This is Page Header!!!';
}
private function ppt()
{
echo 'blah blah';
}
}
$k = new Kcontroller();
$k->index();
$k->render();
echo $k->output();
你了解一下php public protected private作用域就明白了