I am setting a parents class properties like so:
class Parent {
protected $object;
protected $childObject;
function __construct() {
$this->object = new Object();
//I can access the objects methods here
$this->childObject = new Child();
}
}
but when i try to access in the child I get all to a member function method() on a non-object when i try to access the method.
class Child extends Parent {
function __construct() {
$this->object->method();
//But here I just get NULL
}
}
class Object extends Parent {
public function method() {
//do stuff
}
}
And the parent class is being initiated after all the classes are declared.
You also have to call the parent constructor in order to assign the object to the property, like this:
class Child extends Parent {
function __construct() {
parent::__construct();
//^^^^^^^^^^^^^^^^^^^^^^ See here I call the constructor of the parent
$this->object->method();
}
}
For more information about constructors and destructors see the manual: http://php.net/manual/en/language.oop5.decon.php
Related
Suppose i call the parent class of my subclass with parent::__construct(); .How can i detect whether the parent class have been called by a subclass in the parent class
I don't know if I understood your question but my suggestion is to set a static variable that's true, in my case $CHILD, in the Child class then use get_called_class in the Parent class and then test the aforementioned static variable.
class Base {
public function __construct() {
$child = get_called_class();
if($child::$CHILD)
{
echo "Parent has being called";
}
}
}
class Child extends Base {
public static $CHILD = true;
public function __constructor()
{
parent::__constructor();
}
}
$child = new Child();
Another approach would be to use debug_backtrace as #icecub suggested
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
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
I have a strange issue where I set values in a parent class but cannot access those values in a child class extending the parent.
class Parent
{
protected $config;
public function load($app)
{
$this->_config();
$this->_load($app);
}
private function _config()
{
$this->config = $config; //this holds the config values
}
private function _load($app)
{
$app = new $app();
$this->index;
}
}
class Child extends Parent
{
public function index()
{
print_r($this->config); // returns an empty array
}
}
$test = new Parent();
$test->load('app');
when I do that i get an empty array printed out. But if I do this then I am able to access those config values.
private function _load($app)
{
$app = new $app();
$app->config = $this->config
$app->index;
}
and
class Child extends Parent
{
public $config;
....
}
then I can access the config data from the parent.
You are accessing the values before anything is initialized there. First you have to set the values.
Example: call a method is the parent class, which set the value, on the contructor of the child class.
class Child extends Parent
{
public function __construct() {
$this -> setConfig(); //call some parent method to set the config first
}
public function index()
{
print_r($this->config); // returns an empty array
}
}
Update: You also seem to confused about concept of OOP
class Parent { ..... }
class child extends Parent { ..... }
$p = new Parent(); // will contain all method and properties of parent class only
$c = new Child(); // will contain all method and properties of child class and parent class
But, you have to work with parent methods and properties just the same way you would do in the normal object.
Lets See another example:
class Parent {
protected $config = "config";
}
class Child extends Parent {
public function index() {
echo $this -> config; // THis will successfully echo "config" from the parent class
}
}
But another example
class Parent {
protected $config;
}
class Child extends Parent {
public function index() {
echo $this -> config; //It call upon the parent's $config, but so far there has been no attempt to set an values on it, so it will give empty output.
}
}
It's because the property in the parent is protected. Set it to public and you can access it in child classes. Or alternatively, create a method in the parent class which returns the config:
public function getConfig()
{
return $this->config;
}
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?