Welcome Guest, Not a member yet? Register   Sign In
How to call a parent-class method in child class
#1

[eluser]lesssugar[/eluser]
The problem I have was originally posted on StackOverflow, however I got no good answer. Hopefully, this forum will be more helpful.

Display original post

Example I provided is based on session but please keep in mind that it's just one of the options.

I also tested this:

Code:
class MY_Controller extends CI_Controller {


public function __construct() {
  parent::__construct();  
  $this->displayName();
  $this->setSession();
}

protected function displayName() {
  echo "John";
}

protected function setSession() {
  $this->session->userdata('name', 'John');
}

}

Code:
class Test extends MY_Controller {

public function __construct() {
  parent::__construct();
}

public function index() {
  var_dump($this->session->userdata('name'));
}

}

In the code above, test/index always displays "John", so parent's <b>displayName()</b> method is called. However the session variable set in <b>setSession()</b> is not there, which means the method is not called in the Test controller.

How come?

I would like to have a group of methods that set specific information, so the information is available in whole application. I know I could use a library but this should be possible via inheritance - by basing all controllers on a parent that provides all needed settings.
#2

[eluser]InsiteFX[/eluser]
Code:
class MY_Controller extends CI_Controller {

protected static $name;

public function __construct() {
  parent::__construct();  
}

protected function displayName() {
  return $this->name;
}

protected function setSession($name = '') {
  $this->name = $name;
  $this->session->set_userdata('name', $name);
}

}

Code:
class Test extends MY_Controller {

public function __construct() {
  parent::__construct();
}

public function index() {
  $this->setSession('John');
  echo $this->displayName();
  var_dump($this->session->userdata('name'));
}

}

Something like that, it's up to you where you want to set the name at.
#3

[eluser]lesssugar[/eluser]
I checked your code, and, unfortunately, it does not work:

Code:
var_dump($this->session->userdata('name'));

returns bool(false).

The second thing is, there's problem with this part:

Code:
public function index() {
  $this->setSession('John');
}

If I want to have "John" available everywhere, I would rather set the name in the parent, so I don't have to think of setting it in the child controller. Child calls <b>parent::__construct</b>, which should make it possible to access parent's methods or filelds, that were called or set in its constructor.

In my first post it works just fine with <b>displayName()</b> method. I would like to know a way to get other methods working as well - even if they don't echo nor return anything.

E.g. if I was to set Facebook user access token, it would be super convenient to set it once in the parent and let all child classes inherit the access token. Of course it could be overwritten if needed, but the default one would be accessible across the whole app.
#4

[eluser]InsiteFX[/eluser]
There I updated my post witch was something you should have seen.

It's set_userdata not userdata, but that's what happens when you copy and paste someone elses' code

#5

[eluser]lesssugar[/eluser]
Of course, it was my mistake - sorry for that. But as I wrote, this was just an (wrong) example.

In the end, I want to inherit parent class settings across whole app. How can I do that?

Let's say the parent class sets some data. I don't want to call the same methods in 10 child controllers - the data should be inherited. It's all I want to achieve.

Is this possible?
#6

[eluser]InsiteFX[/eluser]
For things I need across the whole app I use a registry class, Search the forums me and another user posted two different versions of a registry class. The difference between his and mine is that mine allows you to store multiple arrays.

If you can not find it try to catch me on Skype in the morning and I'll send it to you.




Theme © iAndrew 2016 - Forum software by © MyBB