1

I have good oop understanding but poor understanding of its implementation in php...

I have the following code, hope it's self documented =).

I need to have BB in the output

class A{
    // I can't copy function::classname() to all my descendant classes

    static function classname(){
        echo __CLASS__;
    }
}

class B extends A{

    static function test(){
        self::classname();
    }

    function test1(){
        self::classname();
    }



    //i have A LOT of static and non-static functions using self::classname() in their code
    // I can't copy all them to base class
    }

    $v = new B();
    B::test();
    $v->test1();

I'm stuck with static:: and self:: syntax


PS: another crazy question I've come across:

Suppose I have

function doSomething(){
    echo $this->id;
}

Sometimes it gets into the static contexts. Yes, I know, that's because my bad application design. But is it possible to create a second(mirror, overloading) function

static function doSomething(){
    echo false;
}

It means that using $obj->doSomething() returns id and using Class::doSomething() returns false


Question 3:

Is it possible to get property default value in static context an property value in non-static context automatically?

3 Answers 3

5

Have a look at late static binding.

class A {
    static function classname() {
        echo __CLASS__;
    }

    static function test1() {
        static::classname();
    }
}

class B extends A {
    static function classname() { 
        echo __CLASS__;
    }
}

$v = new B();
B::test1();
$v->test1();

Or as pointed out by Long Ears in the comments, assuming php 5.3.0+ you can use get_called_class()

class A {
    static function classname() {
        echo get_called_class();
    }

    // this can be defined in either class A or B without affecting the output
    static function test1() {
        static::classname();
    }
}

class B extends A {
}

$v = new B();
B::test1();
$v->test1();

Outputs:

BB
Sign up to request clarification or add additional context in comments.

5 Comments

doesn't this example output AA?
My bad, updated the question. You need to use late static binding.
If you want to get the called class in classname() without implementing it again in the child, you can change it from echo __CLASS__; to echo get_called_class(); (Requires PHP >= 5.3.0 like LSB)
I'm sorry, but that's not what I want. The last edit explains why
@LongEars Would it be difficult for you to post the entire solution?
2

Regarding your second "crazy" question, see the Magic Methods. Basically, you would need to implement something like:

class Foo
{
  public function __call($name, $arguments)
  {
    // call the _$name function
  }

  public static function __callStatic($name, $arguments)
  {
    // call the _{$name}_static function
  }

  private function _bar()
  {
  }

  private static function _bar_static()
  {
  }
}

$foo = new Foo();
$foo->bar();
Foo::bar();

Comments

0

it's possible to add a static method like this

class Foo {   
public static function __callStatic()   {
// ....     
}
}

// in Other file 

// Call the static method   
Foo-->__callStatic()

and call it on an other file (In php ) ?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.