1

I have code two classes

class old{
    function query(){
        #do some actions ...
    }

    function advancedQuery(){
        #some actions and then
        $this->query();
    }
}

and

class awesome extends old{
   function query(){
        parent::query();

        $fromClassOld = false; # this variable must be changed if method is called from class 'OLD'
        if( $fromClassOld ){
            echo 'foo ';
        }else{
            echo 'bar ';
        }
   }
}

then some usage like this

$awesome = new awesome();
$awesome->query(); # should return 'bar'
$awesome->advancedQuery(); # should return 'foo'

You can check code here http://goo.gl/Gq63Wk

I want that variable $fromClassOld to be changed if method query() is called from class 'old'.

Class old cant be modified.

I found hack by using debug_backtrace() and checking variable debug_backtrace()[0]['file'], but I am preaty sure this could be solved in better way.

  • Simple answer - there's no way. Another question - what do you need this for? – u_mulder Jan 18 '15 at 19:15
  • You said you are unable to change the behavior of `class old`, but you are in effect trying to change it in the subclass. You can tell it to take a different action in the subclass' extending method, but by definition no code from the subclass could be called if you a similar method in the parent. – Michael Berkowski Jan 18 '15 at 19:16
  • wow i am surpised that this can't be done. I am replacing old database class in project. But I want to maintain old functionality. Main problem new and old classes has method query() but it functions in different way. – Linas Baublys Jan 18 '15 at 19:31

2 Answers2

1

You cannot. If you call method on child class, then you call method on child class. If you call method on parent class, it has no acces to child's methods. so your advancedQuery() never goes to the awesome::query(), but only to old::query()

All you can do is make another advancedQuery() in awesome class.

Forien
  • 2,712
  • 2
  • 13
  • 30
0

Best thing I could make :

$bt = debug_backtrace();
end($bt);
var_dump($bt[key($bt)]['class']);