php calling parent function makes parent unable to load own vars - php

I have a Handler class that looks like this:
class Handler{
public $group;
public function __construct(){
$this->group = $this->database->mysql_fetch_data("blabla query");
//if i print_r($this->group) here it gives proper result
new ChildClass();
}
public function userGroup(){
print_r($this->group); //this is empty
return $this->group;
}
}
class ChildClass extends Handler{
public function __construct(){
$this->userGroup();
//i tried this too
parent::userGroup();
//userGroup from parent always returns empty
}
}
Workflow:
Handler is called from my index.php and the __construct is called
Handler needs to create $group
Handler creates child class
Child class calls Handler function
When I try to return $group in the function It tries to get $this->group from Child instead of Handler
Whenever I try to ask the parent something I can only access the parent function then inside the function the parent class can't find any of it's own variables
EDIT:
I figured using 'extends' would be useful in calling parent functions but it seems just passing $this on to the child will be easier.

You never called the parent constructor, so the group object is never initialized. You will want to do something like this.
class Handler{
public $group;
public function __construct(){
$this->group = $this->database->mysql_fetch_data("blabla query");
//if i print_r($this->group) here it gives proper result
new ChildClass();
}
public function userGroup(){
print_r($this->group); //this is empty
return $this->group;
}
}
class ChildClass extends Handler{
public function __construct(){
parent::__construct();
$this->userGroup();
}
}
If you had not overwritten the __construct method in your extended class, then the parent __construct would have automatically been called, but since you overwrote it in the extended class, you must tell it to call the parent's __construct in your extended class' __construct.

Related

Parent and child classes not referencing correctly within a function PHP

I am building an MVC component and I'm getting stuck with an issue with a parent and child model. I have a few methods in the parent Model and they're not working with the database_class object
the constructor works fine
but when I use that object in the methods its like the constructor doesn't exist?
Class Controlller
{
public function __construct()
{
$this->childModel = $this->model('childModel');
} // end construct
// methods go here
}
Here are the models:
class childModel extends parentModel {
private $dbo;
public function __construct()
{
$dbobj = new Database_class;
$this->dbo = $dbobj;
}
//methods
}
class parentModel {
private $dbom;
public function __construct()
{
$dbombj = new Database_class;
$this->dbom = $dbombj;
var_dump($this->dbom); //working perfectly as database object
}
public function methodName()
{
var_dump($this->dbom); //not showing up as database object
}
}
I don't think this code is doing what you think it's doing. In childModel, you are overwriting the __construct method of the parentModel, so the __construct in the parentModel never gets called. Therefore $this->dbom should be null. Furthermore if you wish to use $this->dbom from the childModel, you should probably change the scope from private $dbom to protected $dbom. See this page for more info on that: http://php.net/manual/en/language.oop5.visibility.php

PHP - Extends class modify protect parent

I created an extended class in order to modify a protected var for particular purpose. However I don't understand how I can modify a parent class protected var from a child class and use it everywhere in the parent functions.
For example:
class parent {
protected $data;
public function __construct() {
add_action('wp_ajax_output', array(&$this, 'output'));
}
public function output() {
get_data();
show();
}
public function get_data() {
$this->$data = 'data_1';
}
public function show() {
// here I'm using the protected var (I would like to use it from child)
echo $this->data;
}
}
new parent();
class child extends parent {
public function __construct() {
parent::__construct();
add_action('wp_ajax_child_output', array(&$this, 'child_output'));
}
public function child_output() {
$this->data = 'data_2';
// I would like to use $this->data in parent::show();
parent::show();
}
}
new child();
How can I override all protected var use in parent?
I've just tested your code and it gives out correct output. Issue lies somewhere else.
when you use child_output() you use it correctly. All protected properties are accessible directly through a child class as well as all protected,public methods.
if you create an object from the parent class then you are using the parent class as it is. If you create an object from the child class then it inherits all properties and methods from the parent class, thus child class has a property $data and three extra methods

php overriding method called by constructor in child class

I was playing around with a couple of classes to understand the relationship between parent and child. I setup the parent to have a constructor that calls an init method. Then when I add an init method to the child, it should override the parent init, shouldn't it? But what is happening is that both methods are being called.
To test this, I wrote a class named Model and a child called Instance. Here is the code:
$try = new Instance;
echo $try;
class Model{
public function __construct(){
$this->init();
}
public function init()
{
return $this->className();
}
public function __toString()
{
return $this->className();
}
public static function className()
{
return get_called_class();
}
}
class Instance extends Model
{
public function init()
{
echo "tada! ";
}
}
Gives the following output:
tada! Instance.
In class Model, I use the magic method __toString() to return the class name as a string. The constructor of the parent calls the parent init() method, which in this case echoes the class name.
My understanding is that if I write a child class, in this case the class is called Instance, with an init() method, it would overwrite the parent init() method, but that is not what is happening. In this case, it returns both init mehtods and I have no idea why. Can anyone explain this?
The fact is -
When you instantiate an object of Class "Instance" using "$try = new Instance;". it calls the constructor and since child class overrides "init" method, it prints "tada!".
On the other line you echo the object of Class "Instance" using "echo $try;" and a magic method __toString() implemented in parent class. So it print "Instance" as class name.
You can run only $try = new Instance; that will print only "tada!".
You are running two different commands. First you are instantiating the class with $try = new Instance; which is calling __construct. The __construct() in the parent class is calling init() which is using the child init() and returning tada!. Create a blank constructor in the child class to override the parent constructor.
The second command is when you echo the class: echo $try; using __toString() which is echoing Instance as defined in the parent class.
You can also add to a parent constructor by using:
public function __construct() {
...code...
parent::__construct();
...code...
}
Reason :
Since you don't have any constructor defined for your child class, the parent class constructor will be fired and that is why you get that output.
Explanation :
When you do
$try = new Instance;
The parent class constructor gets fired and it will call the init() which in turn calls the className() that does the get_called_class() , so eventually your init() in the child class will be called and you get the output "tada!" first.
And when you do..
echo $try;
The __toString() is called and returns you the class name Instance
So basically , If you had a constructor on your child class , you will not be getting the output "tada!"
In this case, it returns both init mehtods
No it doesn't;
'tada!' is being echoed by Instance::init() and 'Instance' is being echoed by Model::__toString().

How do I access a parent's methods inside a child's constructor in PHP?

Say I have class child() and class parent(). The parent has a constructor and a few other public methods, and the child is empty apart from a constructor.
How do I go about calling a parent's methods inside of the child's constructor, as in:
Class Parent {
public function __construct() {
// Do stuff (set up a db connection, for example)
}
public function run($someArgument) {
// Manipulation
return $modifiedArgument;
}
}
Class Child extends Parent {
public function __construct() {
// Access parent methods here?
}
}
Say I want to call parents run() method, do I have to call a new instance of the parent inside the child constructor? Like so...
$var = new Parent();
$var->run($someArgument);
If so, what is the point of extends from a class definition POV? I can call a new instance of another class with the new keyword whether it extends the 'child' or not.
My (likely) wrong understanding was that by using extends you can link classes and methods from a parent can be inherited into the child. Is that only outside the class definition? Does using extend offer no efficiencies inside the class definition?
Because referring to the parent's run() method with the this keyword certainly doesn't work...
Use parent as predefined reference: parent::run(). This will ensure you call parent method. The same way you could call first parent constructor first or after child one - parent::__construct().
Class Child extends Parent {
public function __construct() {
parent::__construct();
// Access parent methods here?
$some_arg = NULL; // init from constructor argument or somewhere else
parent::run($some_arg); // explicitly call parent method
// $this->run($some_arg); // implicitly will call parent if no child override
}
}
If you dont have an implementation in child you could call $this->run($args), where it will again call parent run method.
To extend Rolice's answer
function a() {
echo 'I exist everywhere';
}
class A {
protected $a
function a() {
$this->a = 'I have been called';
}
function out() {
echo $this->a;
a();
}
}
class B extends A {
function __construct() {
parent::a();// original method
$this->a(); // overridden method
a();
}
function a() {
$this->a = $this->a ? 'I have been overwritten' : 'first call';
}
}
Study these to understand the difference

Access parent properties in child using $this

I am trying to create a simple MVC my personal use and I could really use an answer to this simple question
class theParent extends grandParent{
protected $hello = "Hello World";
public function __construct() {
parent::__construct();
}
public function route_to($where) {
call_user_func(array("Child", $where), $this);
}
}
class Child extends theParent {
public function __construct() {
parent::__construct();
}
public function index($var) {
echo $this->hello;
}
}
$x = new theParent();
$x->route_to('index');
Now Child::index() this throws a fatal error: Using $this when not in object context but if I were to use echo $var->hello, it works just fine.
I know I can use $var to access all properties in the parent, but I would rather use $this.
By writing call_user_func(array("Child", $where), $this) you are calling the method statically. But as your method isn't static you need some kind of object instance:
call_user_func(array(new Child, $where), $this);
Documentation on callback functions.
You don't have an instance of Child to call a non-static method upon when you're doing $x->route_to('index'); The way you're calling the method, without having made an instance first, is implied static.
There are two ways to correct it. Either make the Child class's methods static:
class Child extends theParent {
public function __construct() {
parent::__construct();
}
static public function index($var) {
echo self::$hello;
}
}
...or make an instance of the child class for the parent to use:
class theParent extends grandParent{
protected $hello = "Hello World";
private $child = false
public function __construct() {
parent::__construct();
}
public function route_to($where) {
if ($this->child == false)
$this->child = new Child();
call_user_func(array($this->child, $where), $this);
}
}
Of course, both of these samples are rather generic and useless, but you see the concept at hand.
$this gives you access to everything visible/accessible in the current object. That can either be in the class itself (this) or any of it's parents public or protected members/functions.
In case the current class overrides something of a parent class, you can access the parent method explicitly using the parent keyword/label, whereas you add :: to it regardless if it is not a static method.
Protected variables exist only once, so you can not use parent to access them.
Is this info of use?

Categories