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.
Related
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;
}
}
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
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();
I mean something like that:
class parentClass {
public function method() {
echo $this->prop;
}
}
class childClass extends parentClass {
public $prop = 5;
}
How can I get a child prop from the parent prop?
Thanks...
Either I don't fully understand what you want or the solution is as trivial as the following code.
class parentClass {
public function method() {
echo $this->prop;
}
}
class childClass extends parentClass {
public $prop = 5;
}
$object = new childClass();
$object->method();
I mean the child class is extending the base class which means it will also inherit all the methods of its parent's class. That makes the whole process of using the parent's class method as simple as calling it from the instance of the child class.
All protected and public members of child classes are visible from within their parent class in PHP, so the example code you provided should work just fine. Quote from the php doc:
Members declared protected can be accessed only within the class
itself and by inherited and parent classes.
But the actual question is: do you really need it?
The proper OO way would be to define a self-contained parent class that expresses something. It should not need to access properties of child classes - this is a so-called code smell. If you really think that you have a case where a similar construct is necessary, you are probably looking for abstract methods, which guarantee that every child class has this property:
abstract class Animal {
public function makeNoise() {
echo $this->getNoiseString();
}
protected abstract function getNoiseString();
}
class Cat extends Animal {
protected function getNoiseString() {
return 'meow';
}
}
//parent
class parentClass {
protected $prop = null;
public function method() {
echo $this->prop;
}
}
//child
class childClass extends parentClass {
protected $prop = 5;
}
Make sure the variable is defined in the parentclass as well. So it will be accessible by the parent.
I want to do something like the following:
class SomeOtherClass {}
class Test
{
public $member = new SomeOtherClass();
}
The only problem is that I do not want to use a constructor, because the 'Test' class should extend another class and should not override the constructor.
Is this actually possible in PHP?
You can extend parent constructor in Test like this:
class Test extends SomeClass
{
public $member;
function __construct() {
$this->member = new SomeOtherClass();
parent::__construct();
}
}