views:

265

answers:

4

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?

+1  A: 

Use php's parent keyword.

PHP parent

KB22
Where though? Where am I putting the keyword?
James Brooks
Check:http://stackoverflow.com/questions/393204/using-parent-variables-in-a-extended-class-in-phpThere the topic is discussed in detail.
KB22
A: 

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)

Jörg
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.
James Brooks
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.
Jörg
A: 

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.
    }
}
Tom Haigh
That's brilliant! Thank you Tom!
James Brooks
A: 

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

knittl
But where? If I've still got the function in the extended class, where?
James Brooks
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`
knittl