How do I call a static child function from parent static function?
Asked Answered
R

3

13

How do I call child function from parent static function ?

In php5.3 there is a built in method called get_called_class() to call child method from parent class. But my server is running with php 5.1.

Is there any way can do this ?

I want to call it from a static function . So that I can not use "$this"

So i should use "self" keyword.

Below example my parent class is "Test123" , from the parent class static function "myfunc" am trying to call child class function like this "self::test();"

abstract class Test123
{

  function __construct()
  {
    // some code here
  }

  public static function myfunc()
  {
    self::test();
  }

  abstract function test();
}

class Test123456 extends Test123
{
  function __construct()
  {
    parent::__construct();
  }

  function test()
  {
    echo "So you managed to call me !!";
  }

}

$fish = new Test123456();
$fish->test();
$fish->myfunc();
Rhinitis answered 13/7, 2011 at 11:57 Comment(6)
you should show us snippet of your codeTypewriter
Just use $this->parentFunc(). And no, get_called_class() has not been introduced to call parent functions. -- php.net/manual/en/language.oop5.inheritance.phpWintery
possible duplicate of PHP: How to call function of a child class from parent classPilfer
My parent function is staticRhinitis
@Sahal: There is no late static binding in PHP 5.1, you need to explicitly name the class to call the function: Test123456::test(), self will be Test123. - see my updated answer for an explanation.Wintery
@jeni: That's not a duplicate question, this one is about static functions.Wintery
W
24

Edit: What you try to achieve is not possible with PHP 5.1. There is no late static bindings PHP Manual in PHP 5.1, you need to explicitly name the child class to call the child function: Test123456::test(), self will be Test123 in a static function of the class Test123 (always) and the static keyword is not available to call a static function in PHP 5.1.

Related: new self vs new static; PHP 5.2 Equivalent to Late Static Binding (new static)?


If you are referring to a static parent function, then you need to explicitly name the parent (or child) for the function call in php 5.1:

parentClass::func();
Test123456::test();

In PHP 5.3 you can do this instead with the static keyword PHP Manual to resolve the called class' name:

static::func();
static::test();

If those are non-static, just use $this PHP Manual:

$this->parentFunc();
$this->childFunc();

Or if it has the same name, use parent PHP Manual:

parent::parentFunc();

(which is not exactly what you asked for, just putting it here for completeness).

Get_called_class() has been introduced for very specific cases like to late static bindings PHP Manual.

See Object Inheritance PHP Manual

Wintery answered 13/7, 2011 at 12:3 Comment(1)
OT: How do you create the documentation links so consistently? I've seen this on several users already, do you use some sort of script for that?Budgerigar
C
0

I suspect you are a bit confused abuot parent / child, class / object and function / method.

Ionuț G. Stan has provided the explanation of how to invoke a method which is not declared in a parent class (which as he says should be abstract or implement the __call() method).

However if you mean how do invoke a method which has been overridden in a child class from the parent, then it is not possible - nor should it be. Consider:

Class shape {
 ...
}

Class circle extends shape {
  function area() {

  }
} 
Class square extends shape {
  function area() {

  }
}

If it is your intent to call the area method on an instance of 'shape' (which does not have an area method) then which child should it use? Both the child methods would depend on properties which are not common / not implemented by the shape class.

Cleptomania answered 13/7, 2011 at 12:7 Comment(0)
E
0

try this:

<?php
 class A {
 public static function newInstance() {
$rv = new static();  
return $rv;
}

public function __construct() { echo " A::__construct\n"; }
}
class B extends A {
public function __construct() { echo " B::__construct\n"; }
} 
class C extends B {
public function __construct() { echo " C::__construct\n"; }   
}
?>
Epineurium answered 13/7, 2011 at 12:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.