PHP:从继承的类访问类属性
我试图在构造函数中创建类属性,然后在扩展类中访问它们,但在获取它们时遇到了困难 这是我的设置,抱歉,很多代码PHP:从继承的类访问类属性,php,object,Php,Object,我试图在构造函数中创建类属性,然后在扩展类中访问它们,但在获取它们时遇到了困难 这是我的设置,抱歉,很多代码 class Object { function __autoload($c){ strtolower($c); include_once('lib/'.$c); } function __construct() { } } class Rules extends Object{ function __aut
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::vouel
更改为parent::$vouel
,并将其设置为static
,但它仍然没有读取正确的值
我是否需要在构造函数中实例化
$rule=new Rules()
的新副本?或者应该翻译自动加载,因为它正在扩展父级?在您的Translate
类中,父级::$vouel
将引用规则
类中的静态属性
您的属性不是静态的,所以您应该使用
$this->元音这在很多级别上都是错误的。请删除基本对象、空的构造函数、错误继承、多余的自动加载、全局和静态。你不是在做面向对象编程,你是在做基于类的编程。你从不把strtolower($c)
的结果分配给任何东西。也许你的意思是$c=strtolower($c)
@Gordon请你给我推荐一个好的起点好吗?试试这个,然后点击链接。OOP一开始很容易,但即使经过多年的学习,也很难做到正确,所以不用担心:)另外,看看你所在的领域是否有PHP用户组来讨论类似的话题。@Gordon谢谢!我将阅读并重新开发我的应用程序