您好我有一个类:AbstractEntityType,带有受保护的属性:
abstract class AbstractEntityType extends AbstractType {
protected static $lists = null;
public function __construct($lists = array()) {
AbstractEntityType::$lists = $lists;
}
public function configureOptions(OptionsResolver $resolver) {
$resolver->setRequired(array(
'temp', 'statut'
));
}
}
这是另一个扩展前一个类的类:
class MyType extends AbstractEntityType {
....
}
我使用工厂创建MyType类:
class SimpleFormTypeFactory {
public function createType($entity_type, $entity_stub, $lists = null) {
$type = null;
switch($entity_type) {
....
case SOMENUMTYP:
$type = new MyType($lists);
break;
}
}
我在本地使用php 5.4和Windows测试它没有问题,但在服务器上(linux和php 5.3)我有这个错误:
Error: Cannot access protected property MyType::$lists
发生了什么事?一个php bug?
谢谢
答案 0 :(得分:5)
该属性受到保护,因此您永远不会这样做:
public function __construct($lists = array()) {
AbstractEntityType::$lists = $lists;
^^^^^^^^^^^^^^^^^^^^^^^^^^ Not allowed for a protected property, regardless where you are
}
但是,当您进入课堂时,可以直接访问它:
public function __construct($lists = array()) {
self::$lists = $lists;
}