OOP - Getting Parent Variable

Hey I can’t figure this out, these OOP books are pretty tough, mercy.

[B]

  1. Is there a way to get a variable from the Parent?[/B]
    Kind of like this…
    echo BaseStats->baseStr;
    echo parent::baseStr; // for non static?

    [B]
  2. Can this be done? A child of a child?[/B]
    class DamageDealer extends CalculatedStats, if Calculated stats is a child of BaseStats?

This is what I have, it’s very simple

<?php

class BaseStats 
{

    public $baseStr;
    public $baseDex;
    public $baseLuck;
    public $baseVitality;
    
    function __construct($baseStr, $baseDex, $baseLuck, $baseVitality)
    {
        $this->baseStr = $baseStr;
        $this->baseDex = $baseDex;
        $this->baseLuck = $baseLuck;
        $this->baseVitality = $baseVitality;
    }
    
    function getBaseStats ()
    {
        return "
        str: {$this->baseStr} <br />
        dex: {$this->baseDex} <br />
        luck: {$this->baseLuck} <br />
        vitality: {$this->baseVitality} <br />
        ";
    }
    

}

class CalculatedStats extends BaseStats 
{

    public $calcStr;
    public $calcDex;
    public $calcLuck;
    public $calcVitality;
    
    function __construct($str, $dex, $luck, $vitality)
    {
        parent::__construct($str, $dex, $luck, $vitality);
    }
    

    function getCalculatedStats()
    {
        return "
        Cstr: {$this->calcStr} <br />
        Cdex: {$this->calcDex} <br />
        Cluck: {$this->calcLuck} <br />
        Cvitality: {$this->calcVitality} <br />
        ";
    }

}


$ee = new CalculatedStats(1,2,3,4);
echo $ee->getBaseStats();
echo $ee->getCalculatedStats();

It’s automatically included as part of the class.
For example:

<?php
Class ClassA{
    public $Something = 'hello';
}
Class ClassB{
    function out(){
        echo $this->Something;
    }
}
$classB = new ClassB;
$classB->out();

A child class automatically gets the variables and methods used in the parent.


<?PHP
Class ClassA{

    public $Something = 'hello';
        private $a = "aaaa";
        public function get_a()
        {
                echo $this->a;
        }

}

Class ClassB extends classA{

    function out(){

        echo $this->Something;

    }

}

$classB = new ClassB;

$classB->out();

$classB->get_a();

I just wanted to add, that only public and protected variables are accessible from the subclass or derived class or child class.

Accessing member variables directly, especially in parent classes can lead to very confusing code and is considered a bad practice by some. It’s better to provide simple mutator/accessor methods and use those every where instead of directly accessing the member data.

There are other advantages as well, with the disadvantage being (some might argue) somewhat slower, due to the extra code. Small trade off for cleaner code IMHO.

The only time you would use parent:: is not when accessing statics but when you explicitly want to call a parent method as opposed to any overrided methods you might provide in the subclass¨.

Cheers,
Alex

Thank you guys, I see it is automatically inherited now so I can use $this->fromParentClass.

PCSpectra – You are saying its better to use an mutator/accessor (Not sure what that means), but I’d like to use the best methods practiced.

So for example, are you suggesting that calling this variable from the parent class is considered bad:

 $this->baseStr

Then are you proposing something like this which uses a method to retrieve the variable from its parent:

class A {
   public $baseStr;

   // Pretend we have a construct or setter for $baseStr right here..

   public function returnStr() {
      return $this->baseStr;
   }
}

Class B extends A {
  public $newStrength;

  function __construct ()
  {
      $this->newStrength = returnStr();
  }
}

So for example, are you suggesting that calling this variable from the parent class is considered bad:

Not in itself bad, but it can lead to harder to follow code, more fragile code, yes.

Accessor-Mutator are horrible names, sorry. The are much more commonly known as setter and getter methods, that basically encapsulate the member data and provide a function interface to variables in parent classes, etc.

So if the parent class had a variable labeled name your getter and setter methods might look like this:

class Parent{

  private $name = '';

  public function getName()
  {
    return $this->name;
  }

  public function setName($name)
  {
    $this->name = $name;
  }

}

You would typically provide a getter/setter method for every member variable and keep them private to prevent anything outside the context of that parent from tampering with the data.

Yes it’s more work, but really only typing as the logic is not complex and the extra layer of abstraction can really come in handy.

Cheers,
Alex

If the idea of too many accessors gives you the horrors you can always take a look at the “magic methods” __get() and __set().

Although there are pros and cons with them too, depending on whether you see all those getName, setName methods as part of your documentation, you are doing other processing etc.

http://www.php.net/manual/en/language.oop5.overloading.php#language.oop5.overloading.members

JReam,

Two things strike me about your example. First of all, I’m not quite sure that you need inheritance in that example. Do they need to be two classes? Can you use calculated stats as a wrapper or decorator for base stats?

Also, you might want to separate your getBaseStats style functions that are meant to produce output into a separate class.

I agree here.

I prefer to make my classes more ‘free’. With programming languages such as C# and PHP, it is common convention to have public properties and, if needs be, setter/getter functions which are processed on accessing that property.

However, with languages such as Java, its common practice to use getter and setter functions.

Whilst there is benefit in terms of stability from using getSomething() and setSomething($Value), alot of time they are unnecessary.

I don’t see much point in having getter and setter functions for every property if you aren’t going to do anything other than set a value or return it. It’s more of an annoyance than anything else.

Thank You Guys.

Okay the Getter/Setter method for passing things makes good sense. I will defininately look at thes magic __get/__set because I foresee a lot of variables passing through.

As far as extending the class, I’m not sure. This was a basic example I wrote up before posting so nothing that serious. I know I could put it all functions/variables in one class too and make it store all the Base and Calculated stats. But re-usability is key and I know that can be a bad idea.

@Selkirk – So, sorry for my unexperience but let me understand this. Would a wrapper/decorator would that be something like this… Where you drop in a object into a method?


class B {
  public $str;

  __construct($objA){
  $this->str = $objA->str;
}

}