0

我正在尝试从扩展类中检索变量。这是我的主要课程的外观:

class SS {
    public $NONE = NULL;
    public $NUMBERS = array("1", "2", "3", "4", "5", "6", "7", "8", "9", "0");
    public $OPERATORS = array("=", "&&", ">", "<", "+", "-", "/", "*", "^");
    public $DBLQUOTES = '"$1"';
    public $SNGQUOTES = "'$1'";
    public $CODE;

    function SuperSyn($sCode, $cLang) {         
        $hLang = new VB6;
        $VB6 = $hLang->__construct();
        echo $VB6->ssAuthor;
    }
}

我的扩展类看起来像这样(我删除了许多关键字)。

class VB6 extends SS {
    public function __construct() {
        $ssAuthor = "James Brooks";
        $ssCSS = "languages/vb6.css";
        $ssNumbers = $NUMBERS;
        $ssKeywords = array("Abs", "Access", "AddItem");
        $ssReserved = $NONE;
        $ssComments = "('.+)";
        $ssOperators = $OPERATORS;
        $ssDoubleQuote = $NONE;
        $ssSingleQuote = $NONE;
    }
}

如果我删除被调用的公共函数 __construct,PHP 会认为它需要一个函数。

我的问题是,如何将扩展类中的变量检索到我的主类中?

4

4 回答 4

1

Use php's parent keyword.

PHP parent

于 2009-09-01T10:09:18.207 回答
1

The constructor will be called for you when you create an object, so this should work:

function SuperSyn($sCode, $cLang) {                     
     $hLang = new VB6(); //I think you need some parameters here
     echo $VhLang->ssAuthor;
}

However in your VB6 constructor you are currently only assigning local variables, so you wouldn't be able to access ssAuthor externally. Instead you probably want to do something like:

class VB6 extends SS {
    public $ssAuthor;

    public function __construct() {
        $this->ssAuthor = "James Brooks";
        //etc.
    }
}
于 2009-09-01T10:12:03.613 回答
0

I think that there's a logical problem with your expectiation. Why should a function of the class SS know about a variable that only exists in the VB6 subclass?

That would not be a clean inheritance behaviour and reveals a problem with your classes.

Two choices to solve that:

  • Put the variable in the main class to use it in a function in that class
  • Put the function using the subclass's variable inside the subclass

(After reading the comments about the parent keyword and ´$this´ variable: I understood the question differently and don't think either of those to would help since the opposite direction is required: parent class function > subclass variable, not subclass function > parent class variable)

于 2009-09-01T10:11:38.000 回答
0

use parent:: or $this-> (depends, i.e. you have two variables with the same name)

于 2009-09-01T10:13:01.180 回答