PHP object-oriented Inheritance - accessing parent's property - php

I need to access a property of a parent inside a function of the child class. A static variable can accessed with parent:: but how can I access a non-static parent variable when the child class has a variable with the same name?
class My_parent{
$name = "Praeep";
}
class My_child extends My_parent {
$name ="Nadeesha";
function show_name() {
// need to access $name of the parent just referring the parent variable
}
}

You can either declare the variable in the parent class with a protected modifier or provide a getter. The getter approach would be prefered to ensure encapsulation.
class My_parent{
private $name = "Praeep";
public function getName() {
return $this->name;
}
}
class My_child extends My_parent {
public function show_name() {
echo $this->getName();
}
}
If you also want the property to be mutable consider providing a setter as well.

Add a construct function to your parent class and define your variable inside this function.
class My_parent{
public $name;
public function __construct(){
$this->name= "Praeep";
}
}
If your child class has a construct function too, you need to invoke the parents construct function manually. However a class doesn't have to have a construct function so I commented it out for simplicity.
class My_child extends My_parent {
// public function __construct(){
// parent::__construct();
// }
public function show_name(){
echo $this->name;
}
}
$c=new My_child();
$c->show_name();
EDIT:
well in fact you don't need the construct function in the parent class.
class My_parent{
public $name= "Praeep";
}
class My_child extends My_parent {
public function show_name(){
echo $this->name;
}
}
$c=new My_child();
$c->show_name();

Related

How do I make a class variable automatically available to its children classes?

I have an Application class and I also have a number of separate classes that extend it. If I set a $variable in the parent Application class, How do I make it automatically available in its children?
class Application {
public $variable;
public function __construct() {
$this->variable = "Something";
}
}
class Child extends Application {
public function doSomthing() {
$mything = $variable." is cool";
return $mything;
}
}
I know I can put global $variable; in my doSomthing() method, but that is super tedious to do over and over in every method I write. Is there a way to do it where it just is available to all my child class methods?
Thanks.
You just set a property named variable in your Application class in __construct method.
If the property visibility permits (e.g. is public or protected), you can access a property potato in any children classes method with $this->potato:
class Application {
public $variable;
public function __construct() {
$this->variable = "Something";
}
}
class Child extends Application {
public function doSomthing() {
$mything = $this->variable." is cool";
return $mything;
}
}

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

How can I access parent class variable?

I would like to access parent class variable. How can I access parent class variable?
here is example:
class myclass {
var $parentvar;
function __construct() {
$this->parentvar = 'Some text or array';
}
}
class anotherClass extends myclass {
function __construct(argument)
parent::__construct();
}
function print_var() {
echo $parent->parentvar;
}
}
Please suggest me
You can just use $this->parentvar
When you extend a class, all of the public and protected properties and methods become available to the instance scope of your child class.

Is there any way to access Base Class Property via Derived Class object directly in PHP

in php, is there any way to directly access any Base Class Property directly Via an object of a derived Class type.
For eg:
class a
{
public $name="Something";
function show()
{
echo $this->name;
}
};
class b extends a
{
public $name="Something Else";
function show()
{
echo $this->name;
}
};
$obj = new b();
$obj->show();
it'll Print string "Something Else", but what if i wish to access Base class Function show,
it doesn't seem to work like it is done in c++
obj.a::show();
Since you override $name in the child, the property will have the child's property value. You cannot access the parent value then. It wouldn't make sense any other way because the property is public, which means the property is visible to the child (and outside) and modifications to it will change the very base value. So it's effectively one and the same property and value for that instance.
The only way to have two separate properties of the same name is to declare the base property private and the child property non-private and then call a method that has access to the base property, e.g.
class Foo
{
private $name = 'foo';
public function show()
{
echo $this->name;
}
}
class Bar extends Foo
{
public $name = 'bar';
public function show()
{
parent::show();
echo $this->name;
}
}
(new Bar)->show(); // prints foobar
Since your C++ example call is using the scope resolution operator :: you might be looking for class/static properties:
class Foo
{
static public $name = 'foo';
public function show()
{
echo static::$name; // late static binding
echo self::$name; // static binding
}
}
class Bar extends Foo
{
static public $name = 'bar';
public function show()
{
parent::show(); // calling parent's show()
echo parent::$name; // calling parent's $foo
}
}
(new Bar)->show(); // prints barfoofoo

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