PHP:从继承的类访问类属性
我试图在构造函数中创建类属性,然后在扩展类中访问它们,但无法获取它们。
这是我的设置,抱歉,有很多代码。
class Object {
function __autoload($c){
strtolower($c);
include_once('lib/'.$c);
}
function __construct() {
}
}
class Rules extends Object{
function __autoload(){
include_once('lib/rules.php');
}
// Store our set of rules
public static $consonant = '';
public static $vowel = '';
public static $other = '';
function __construct() {
global $_CONF;
// Store a set of rules, as we create them from the config
$this->consonant = '/^[^'.$_CONF['vowels'].$_CONF['additional'].']+/';
$this->vowel = '/['.$_CONF['vowels'].']+/';
$this->other = '/^('.$_CONF['additional'].'+)(.*)/';
}
}
这就是我遇到麻烦的地方
class Translate extends Rules{
function __autoload(){
include_once('lib/translate.php');
}
function __construct() {
parent::__construct();
}
// etc, function()
$return .= preg_replace(parent::$vowel, "$1$2'".$_CONF['vowelending'], $word);
}
警告:preg_replace() [function.preg-replace]:空常规 表达于 D:\xampp\htdocs\PigLatin2\lib\translate.php 第 45 行
现在对我来说,很明显父类属性没有被正确读取。我已将其从 parent::vowel
更改为 parent::$vowel
并将其设置为 static
,但它仍然无法读取正确的值。
我是否需要在构造函数中实例化 $rule = new Rules()
的新副本?或者应该翻译自动加载,因为它扩展了父级?
I am trying to create class attributes in the constructor, and then access them in an extended class, but am having trouble getting at them.
Here is my setup, apologies, lots of code.
class Object {
function __autoload($c){
strtolower($c);
include_once('lib/'.$c);
}
function __construct() {
}
}
class Rules extends Object{
function __autoload(){
include_once('lib/rules.php');
}
// Store our set of rules
public static $consonant = '';
public static $vowel = '';
public static $other = '';
function __construct() {
global $_CONF;
// Store a set of rules, as we create them from the config
$this->consonant = '/^[^'.$_CONF['vowels'].$_CONF['additional'].']+/';
$this->vowel = '/['.$_CONF['vowels'].']+/';
$this->other = '/^('.$_CONF['additional'].'+)(.*)/';
}
}
Here is where I'm having trouble
class Translate extends Rules{
function __autoload(){
include_once('lib/translate.php');
}
function __construct() {
parent::__construct();
}
// etc, function()
$return .= preg_replace(parent::$vowel, "$1$2'".$_CONF['vowelending'], $word);
}
Warning: preg_replace()
[function.preg-replace]: Empty regular
expression in
D:\xampp\htdocs\PigLatin2\lib\translate.php
on line 45
Now to me this is obvious that the parent class attribute is not being read correctly. I have changed it from parent::vowel
to parent::$vowel
and made them static
, but it's still not reading the correct value.
Do I need to instantiate a new copy of $rule = new Rules()
in my constructor? Or should Translate auto load, as it's extending the parent?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在
Translate
类中,parent::$vowel
将引用Rules
类中的静态属性。您的属性不是
静态
,因此您应该使用$this->vowel
。In your
Translate
class,parent::$vowel
would refer to a static attribute, in theRules
class.Your properties are not
static
, so you shoud instead use$this->vowel
.