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
Related
I wanna call a Child Method in Main Class without declare new child();
class main {
function __construct() {
}
public function test() {
}
}
class child extends main {
function __construct() {
}
function childmethod() {
return "test";
}
}
$main = new main();
$main->childmethod();
best wishes
Make class as abstract and declare the method you want to call from child class as abstract.
abstract class main {
function __construct() {
}
public function test() {
return "test";
}
abstract function childmethod();
}
class child extends main {
function __construct() {
}
function childmethod() {
return "childmethod";
}
}
$main = new child();
echo $main->childmethod();// echoes childmethod
echo $main->test();// echoes test
If I correctly understand what you're asking for, you cannot do it and it's a mistake in the project idea.
If you need to call a child method in the parent, it means the method should go in the parent.
There's no way to call a method of an object without instantiate the object, unless you create a static method
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
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 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 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?