1

I'm trying to retrieve a variable from an extended class. This is how my main class looks:

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;
    }
}

And my extended class looks like this (I've removed many of the keywords).

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;
    }
}

If I remove the public function __construct being called, PHP bitches that it's expecting a function.

My question is, how can I retrieve a variable from the extended class into my main class?

4 Answers 4

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.
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

That's brilliant! Thank you Tom!
1

Use php's parent keyword.

PHP parent

2 Comments

Where though? Where am I putting the keyword?
Check: stackoverflow.com/questions/393204/… There the topic is discussed in detail.
0

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

2 Comments

But where? If I've still got the function in the extended class, where?
wait. after reading your code again it doesn’t make sense at all. you hold an derived object in the base class? (SS:SuperSyn that is) and why do you re-apply all your member variables? nevertheless, this should work: $ssNumbers = $this->NUMBERS
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)

2 Comments

Well the idea of this is that you'll be able to create your own classes for different languages, I need to be able to get each of the different syntax rules from the class.
If you'll have to fetch individual information from the subclasses it might be a good idea to make the SS class abstract and add an abstract function like ´getSyntaxRules()´ that each subclass has to implement and that returns the rules for that language.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.