Snorro Jetje
Snorro Jetje

Reputation: 33

How to get variables from a extended class into its parent class

I'm trying to make a table making class, but for different pages I need different kinds of variables. I want to do this by having the general layout in the parent class and then put all the specifics in the extended classes. However, I have no idea how I can get information from an extended class back to the parent class.

Parent class:

class table {
protected $tablename;
protected $mysqli;

function set_name($name) { 
    $this->tablename = $name;  
}

function connectdb($mysqli) {

    $this->mysqli = $mysqli;
}

function make_table($tablename) {
//all table making stuff
//here I want to access the completed variable
}

Extended class:

class tasktable extends table {
public $completed;

function set_completed($completed) {
    $this->completed = $completed;
    echo $completed;
    } 

function get_completed() {
    return $this->completed;
    }
}

Code on the page:

$tasktable1 = new tasktable($tableName);
$tasktable1->connectdb($mysqli);
$tasktable1->set_completed(0);
$tasktable1->make_table($tableName);

Upvotes: 2

Views: 101

Answers (3)

Zoltán Tamási
Zoltán Tamási

Reputation: 12764

You can have an abstract function in the parent class, which retrieves the required variables, and override it in child classes with returning the appropriate ones.

I haven't been working with PHP for a while, I will use some pseudo code here, excuse me for syntax issues.

class Parent {
  abstract getVariables();

  function makeTable()
  {
    var $fields = $this->getVariables();
    // ... do whatever you want
  }
}

class Child extends Parent {
  override function getVariables() {
    return array($this->completed, $this->otherVariable, ...);
  }
}

Upvotes: 0

Ja͢ck
Ja͢ck

Reputation: 173562

It's hard to tell from your definition of the base class, but you can approach this from two sides.

The first option is to make the parent class and the make_table() method abstract and let child classes define the whole method:

abstract class table
{
    // ...

    abstract function make_table($tablename);
}

Then, in your specialised child class you override the make_table() method:

class tasktable extends table
{
    // ...
    function make_table($tablename)
    {
        // all table making stuff
        // you can reference parent::make_table($tablename) if you want
    }
}

Alternatively, you declare methods that make up a table as abstract and you call them from make_table() in the parent class.

abstract class table
{
    abstract function table_part_xyz($name);

    function make_table($tablename)
    {
        // do stuff and call $this->table_part_xyz($tablename);
    }
}

Then, in the child class:

class tasktable extends table
{
    function table_part_xyz($name)
    {
        return 'foobar';
    }
}

Upvotes: 1

edmondscommerce
edmondscommerce

Reputation: 2011

your best bet here is to use abstract getters and setters for all the things you need to be defined in the child class but accessible by the parent class

eg

abstract class parentClass
{

    /**
     * @return mixed
     */
    abstract protected function getThing1();


    /**
     * @return mixed
     */
    abstract protected function getThing2();

    protected function doingThings()
    {
        $thing1 = $this->getThing1();
        $thing2 = $this->getThing2();
    }


}


class childClass extends parentClass
{

    protected function getThing1()
    {
        return 'thing1';
    }

    protected function getThing2()
    {
        return 'thing2';
    }
}

Upvotes: 1

Related Questions