accessing parent property values in child class - php

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;
}

Related

Multi level inheritance in PHP

I'm trying to access and modify data that is in a parent class which is a child of another class.
I've a parent class
class GrandParent {
protected $data = 1;
public function __construct() {}
public function getData() {
return $this->data;
}
}
Below is my first level child
class Child extends GrandParent {
protected $c1Data;
public function __construct() {
$this->c1Data = parent::getData();
$this->c1Data = 2;
}
public function getData() {
return $this->c1Data;
}
}
If I try to instantiate the Child class and do getData(), I get 2 which is normal. I've another class that inherits Child.
class GrandChild extends Child {
protected $c2Data;
public function __construct() {
$this->c2Data = parent::getData();
}
public function getData() {
return $this->c2Data;
}
}
The problem is that if I try to instantiate GrandChild I and get the data I'm getting null. Is it possible to make my GrandChild class inherit $c1Data = 2 and work with it. I want also to be able to use the Child and GrandParent classes on their own and not be abstract.
You're getting NULL because __constructor of a Child class is not invoked and that's why c1Data property is NOT SET. You should explicitly call for Child __constructor:
class GrandChild extends Child {
protected $c2Data;
public function __construct() {
// here
parent::__construct();
$this->c2Data = parent::getData();
}
public function getData() {
return $this->c2Data;
}
}
This is how you add two integers and display the result with Multi
Level Inheritance in PHP.
<?php
/*
Inheritance:
multiple classes
Parent class/child class
senior and junior
child class extends some data or functions of parent class
child class has its own functions
child class can access all public and protected data and functions
*/
//Multi Level Inheritance Every class extends other class
//Parent Class
class A{
//data
var $a;
function setA()
{
$this->a=10;
}
}
//child class
class B extends A{
var $b;
function setB()
{
$this->b=20;
}
}
class Addition extends B{
function add()
{
$this->setA();
$this->setB();
return $this->a+$this->b;
}
}
class Print1 extends Addition{
function print()
{
$this->add();
print("a=".$this->a);
print("<br/>b=".$this->b);
print("<br/>Addtion:".$this->add());
}
}
//make object
$obj1=new Print1();
$obj1->print();
/*
Make Subtraction, multiplication and division classes and print the values as
a=10
b=20
Addtion=30
Subtraction=-10
Multiplication=200
Division:0.5
*/
?>

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 Child Class does inherits empty object

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

PHP Change parent class at runtime?

I have a parent class that depends on whether child class are instantiated.
class GoogleApp {
protected $auth_token;
public function __construct($scopes) {
$this->auth_token = $scopes;
}
}
class Gmail extends GoogleApp {
public function __construct() {
print_r($this->auth_token);
}
}
$googleApp = new GoogleApp('gmail'); // Change the actual class for all child instances
$gmail = new Gmail();
The idea is that all the children use the same auth_token (which is generated on whether the child classes are used - as of now, I'm just manually adding them to whether I included them in my code). Since I have quite a few child classes (like Calendar or Drive), do I have to inject the parent into each child instance or is there an easier way?
If I understand your request correctly, you're pretty close, you just need to declare your property as static.
class FooParent
{
protected static $scope = null;
public function __construct($scope)
{
self::$scope = $scope;
}
public function getScope()
{
return self::$scope;
}
}
class FooChild extends FooParent
{
public function __construct()
{
if (self::$scope === null) {
throw new Exception('Must set scope first.');
}
}
}
$parent = new FooParent('foo');
$child = new FooChild();
echo $child->getScope(), "\n"; // prints "foo"

2 New Instances of a Parent and Child class, need to change Parent variable directly in parent and view in child

PHP
If I create a new instance of a parent class and a new instance of a child class, how can I change the variable in the parent class directly and view the change in the child class?
Take the following code:
class parentClass {
public $varA = 'dojo';
public function setVarA() {
$this->varA = 'something grand';
}
public function getVarA() {
return $this->varA;
}
}
class childClass extends parentClass {
public function useVarA() {
echo parent::getVarA();
}
}
$parentInstance = new parentClass();
$childInstance = new childClass();
$initialVarA = $parentInstance->getVarA(); // should set $initialVarA variable to 'dojo'
$childInstance->useVarA(); // should echo 'dojo'
$parentInstance->setVarA(); // should set $varA to 'something grand'
$changedVarA = $parentInstance->getVarA(); // should set $changedVarA variable to 'something grand'
$childInstance->useVarA(); // should echo 'something grand' but fails to do so...how can I do this?
If you have either a private or a protected variable (member) in the parent then you can access it simply like this from you child class:
$this->varA = ‘something’;
There reason why your child method does not reflect the change, is that child and parent are two different objects in separate memory space. If you want them to share a value you could make it static.
You don’t need to declare it public.
class Parent {
private $varA;
protected $varB;
public $varC;
protected static $varD;
public function getD() {
return self::$varD;
}
public function setD($value) {
self::$varD = $value;
}
}
class Child extends Parent {
public function getA() {
return $this->varA;
}
public function getB() {
return $this->varB;
}
public function getC() {
return $this->varC;
}
}
$child = new Child();
$child->getA(); // Will not work since $varA is private to Parent
$child->getB(); // Works fine because $varB is accessible by Parent and subclasses
$child->getC(); // Works fine but ...
$child->varC; // .. will also work.
$child->getD(); // Will work and reflect any changes to the parent or child.
If you don’t want all instance of the parent class to share values. You could pass on the parent or child to either new instance and through and update the values of all the related objects accordingly.
$parent->addChild(new Child());
And in the set method:
$this->varA = $value;
foreach ($this->children as $child) {
$child->setVarA($value);
}
Hopes this helps.

Categories