0

say I have :

File BaseFoo.php :

require('MyClass1.php')
class BaseFoo(){

  function check(){
     //do somme stuff
  }

  function f1($o){
     $c = new MyClass1();
     return $c->f1($o);
  } 

}

File MyClass1.php :

MyClass1(){
   function f1(){
       ???->check() // how to call check() (in initiator BaseFoo class) ?
       // do other stuff
   }
}

Now question is : in MyClass1, function f1, I want to call check() function which is in initiator Class.

Thank for help !

Fred
  • 399
  • 3
  • 12

3 Answers3

0

Because BaseFoo doesn't extend MyClass1, it isn't called a base class. This means that you indeed can't use parent. You need to have an object on which you can call the method. Depending on what you want, you may need to create an object (or pass one) of class BaseFoo in MyClass1 (local or as a member) or you may make BaseFoo::check() static:

class BaseFoo(){

  static function check(){
     // Note: you can only access static members here
     // Calling non-static methods statically generates an E_STRICT level warning. 
  }

  function f1($o){
     $c = new MyClass1();
     return $c->f1($o);
  } 

}

Most likely, however, that's not what you actually want to do. You should ask the question "Who owns what?". Here, BaseFoo owns the MyClass1 object. So you probably want this:

class BaseFoo(){

  function check(){
     //do somme stuff
  }

  function f1($o){
     $c = new MyClass1();
     check();
     return $c->f1($o);
  } 

}

And:

MyClass1(){
   function f1(){
       // Don't check here, the creator should check
       // do other stuff
   }
}

You may need to check for different things both in MyClass1 and in BaseFoo though, and you would have 2 separate check methods for that.

Aleph
  • 1,209
  • 10
  • 19
-1

If you google it you will find your answer, but Check this page

So you can use something like that:

class BaseFoo(){

  public static function check(){
     //do somme stuff
  }

  function f1($o){
     $c = new MyClass1();
     return $c->f1($o);
  } 

}

class MyClass1(){
   function f1(){
       BaseFoo::check() // call static function check from BaseFoo
       // do other stuff
   }
}
-1

you can make function check 'static' so u can call BaseFoo::check()

Haim Evgi
  • 123,187
  • 45
  • 217
  • 223
  • Right, no other way ? Because BaseClass name is made by a define define ('__SERVICE_NAME__','BaseFoo'); So __SERVICE_NAME__::check() is working ? – Fred May 09 '13 at 10:07
  • you can use call_user_func http://stackoverflow.com/questions/2108795/dynamic-static-method-call-in-php – Haim Evgi May 09 '13 at 10:09