I've two classes in 2 files.
First (parent.php):
class Parent{
var $ParentVar;
}
Second (child.php):
class Child{
var $ChildVar = 'im a childvar';
}
How can I set $ChildVar value to $ParentVar for printing 'im a childvar' in Parent class?
Firstly, you need to use inheritance correctly, so:
class Child extends Parent {
Secondly, you should never be accessing the value of a Child from the parent. What you could do, is to define an abstract function in the Parent class which the Child needs to implement, and then code in the Parent could then call the Child's function. For example:
abstract class Parent {
abstract protected function returnSomeValue();
public function outputSomeValue() {
return $this->returnSomeValue();
}
}
class Child extends Parent {
protected $ChildVar = 'im a childvar';
protected function returnSomeValue() {
return $this->ChildVar;
}
}
Related
I have a parent class and child class:
class Parent {
function a() {
$child = new child();
$child->a1();
}
}
class Child extends Parent{
function a1() {
}
}
How can I create object of child class in parent class. And child class is inheriting from same parent class where im trying to initialize child class.
Is this correct??
You cannot name your class Parent this will cause your script to fail due to the following error:
Fatal error: Cannot use 'Parent' as class name as it is reserved in
/var/www/html/class.php on line 2
That's due to the following:
Three special keywords self, parent and static are used to access
properties or methods from inside the class definition.
http://php.net/manual/en/language.oop5.paamayim-nekudotayim.php
So as we renamed the class Parent to Folk, everything works.
<?PHP
error_reporting(E_ALL);
class Folk {
function a() {
$child = new Child();
$child->a1();
}
}
class Child extends Folk{
function a1() {
echo("hi");
}
}
$folk = new Folk();
$folk->a();
?>
this outputs: hi
So to answer your question: Yes, you can create a child object in the parent class. Regardless of this being a good practice or not.
See the following example (PHP)
class Parent
{
protected $_property;
protected $_anotherP;
public function __construct($var)
{
$this->_property = $var;
$this->someMethod(); #Sets $_anotherP
}
protected function someMethod()
...
}
class Child extends Parent
{
protected $parent;
public function __construct($parent)
{
$this->parent = $parent;
}
private function myMethod()
{
return $this->parent->_anotherP; #Note this line
}
}
I am new to OOP and am a bit ignorant.
Here to access the parents property I am using an instance of that class, which seems wrong :S (no need of being i child then). Is there an easy way, so that i can sync the parent properties with the child properties and can directly access $this->anotherP without having to use $this->parent->anotherP ?
As your Child class is extending your Parent class, every properties and methods that are either public or protected in the Parent class will be seen by the Child class as if they were defined in the Child class -- and the other way arround.
When the Child class extends the Parent class, it can be seen as "Child is a Parent" -- which means the Child has the properties of the Parent, unless it redefines those another way.
(BTW, note that "parent" is a reserved keyword, in PHP -- which means you can't name a class with that name)
Here's a quick example of a "parent" class :
class MyParent {
protected $data;
public function __construct() {
$this->someMethodInTheParentClass();
}
protected function someMethodInTheParentClass() {
$this->data = 123456;
}
}
And it's "child" class :
class Child extends MyParent {
public function __construct() {
parent::__construct();
}
public function getData() {
return $this->data; // will return the $data property
// that's defined in the MyParent class
}
}
That can be used this way :
$a = new Child();
var_dump($a->getData());
And you'll get as output :
int 123456
Which means the $data property, defined in the MyParent class, and initialized in a method of that same MyParent class, is accessible by the Child class as if it were its own.
To make things simple : as the Child "is a" MyParent, it doesn't need to keep a pointer to... itself ;-)
This may save you a few hours of searching around.
Remember: Your Child class only inherits the properties DEFINED in the Parent class... So if you instantiate an object using Parent class and then populate it with data, then this data will NOT be available in your child class...
It's super obvious of course, but I'm guessing others may run into the same issue.
A super simple solution is not to extend anything, simply pass the $object of your parent class into your child class through a constructor. This way you have access to all the properties and methods of the object generated by parent class
Example
class child {
public parentObject;
public function __construct($parentObject) {
$this->parentObject = $parentObject;
}
}
If your $parentObject has a public property $name, then you can access it inside the child class with a function like:
public function print_name() {
echo $this->parentObject->name;
}
Is there any way how to determine instance of child in parent class in PHP? Let's say we have this code:
class Parent {
public function InstanceOfChild() {
//What to put here to display "Child class is instance of ChildClass123"?
}
}
class ChildClass123 extends Parent {
//Some code here
}
What I need to do (if it's possible) is create method InstanceOfChild() which will tell me instance of child class, because a lot of classes can be child of my parent one, but I want to (let's say) log, which child call which methods. Thanks for help!
There is a function get_called_class() what you are exactly looking for.
class Parent1 {
public static function whoAmI() {
return get_called_class();
}
}
class Child1 extends Parent1 {}
print Child1::whoAmI(); // prints "Child1"
class Parent {
public function InstanceOfChild() {
$class = get_class($this);
return $class == 'Parent'? // check if base class
"This class is not a child": // yes
"Child class is instance of " . $class; // its child
}
}
Note that calling:
$this instanceof Parent
Will always return true, because parent and children are all instances of class Parent.
You can use get_class. So, you would have to set following code:
echo "Child class is instance of ".get_class($childInstance);
(I'm not a php developer, so the syntax might be wrong)
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.
See the following example (PHP)
class Parent
{
protected $_property;
protected $_anotherP;
public function __construct($var)
{
$this->_property = $var;
$this->someMethod(); #Sets $_anotherP
}
protected function someMethod()
...
}
class Child extends Parent
{
protected $parent;
public function __construct($parent)
{
$this->parent = $parent;
}
private function myMethod()
{
return $this->parent->_anotherP; #Note this line
}
}
I am new to OOP and am a bit ignorant.
Here to access the parents property I am using an instance of that class, which seems wrong :S (no need of being i child then). Is there an easy way, so that i can sync the parent properties with the child properties and can directly access $this->anotherP without having to use $this->parent->anotherP ?
As your Child class is extending your Parent class, every properties and methods that are either public or protected in the Parent class will be seen by the Child class as if they were defined in the Child class -- and the other way arround.
When the Child class extends the Parent class, it can be seen as "Child is a Parent" -- which means the Child has the properties of the Parent, unless it redefines those another way.
(BTW, note that "parent" is a reserved keyword, in PHP -- which means you can't name a class with that name)
Here's a quick example of a "parent" class :
class MyParent {
protected $data;
public function __construct() {
$this->someMethodInTheParentClass();
}
protected function someMethodInTheParentClass() {
$this->data = 123456;
}
}
And it's "child" class :
class Child extends MyParent {
public function __construct() {
parent::__construct();
}
public function getData() {
return $this->data; // will return the $data property
// that's defined in the MyParent class
}
}
That can be used this way :
$a = new Child();
var_dump($a->getData());
And you'll get as output :
int 123456
Which means the $data property, defined in the MyParent class, and initialized in a method of that same MyParent class, is accessible by the Child class as if it were its own.
To make things simple : as the Child "is a" MyParent, it doesn't need to keep a pointer to... itself ;-)
This may save you a few hours of searching around.
Remember: Your Child class only inherits the properties DEFINED in the Parent class... So if you instantiate an object using Parent class and then populate it with data, then this data will NOT be available in your child class...
It's super obvious of course, but I'm guessing others may run into the same issue.
A super simple solution is not to extend anything, simply pass the $object of your parent class into your child class through a constructor. This way you have access to all the properties and methods of the object generated by parent class
Example
class child {
public parentObject;
public function __construct($parentObject) {
$this->parentObject = $parentObject;
}
}
If your $parentObject has a public property $name, then you can access it inside the child class with a function like:
public function print_name() {
echo $this->parentObject->name;
}