PHP:从继承的类访问类属性

发布于 2024-10-29 06:34:29 字数 1497 浏览 0 评论 0原文

我试图在构造函数中创建类属性,然后在扩展类中访问它们,但无法获取它们。

这是我的设置,抱歉,有很多代码。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

同尘 2024-11-05 06:34:29

Translate 类中,parent::$vowel 将引用 Rules 类中的静态属性。

您的属性不是静态,因此您应该使用$this->vowel

In your Translate class, parent::$vowel would refer to a static attribute, in the Rules class.

Your properties are not static, so you shoud instead use $this->vowel.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文