tags:

views:

325

answers:

3

Hi,

sorry for that weird subject but I don't know how to express it in an other way.

I'm trying to access a method from a calling class. Like in this example:

class normalClass {
     public function someMethod() { 
          [...]
          //this method shall access the doSomething method from superClass
     }
}

class superClass {
     public function __construct() {
          $inst = new normalClass;
          $inst->someMethod();
     }
     public function doSomething() {
          //this method shall be be accessed by domeMethod form normalClass
    }
}

Both classes are not related by inheritance and I don't want to set the function to static.

Is there any way to achieve that?

Thanks for your help!

+4  A: 

You can pass a reference to the first object like this:

class normalClass {
    protected $superObject;
    public function __construct(superClass $obj) {
        $this->superObject = $obj;
    }

    public function someMethod() { 
        //this method shall access the doSomething method from superClass
        $this->superObject->doSomething();  
    }
}

class superClass {
    public function __construct() {
          //provide normalClass with a reference to ourself
          $inst = new normalClass($this);
          $inst->someMethod();
    }
    public function doSomething() {
          //this method shall be be accessed by domeMethod form normalClass
    }
}
Tom Haigh
A: 

You have a few options. You can use aggregation like so

class normalClass
{
  protected $superClass;

  public function __construct( superClass $superClass )
  {
    $this->superClass = $superClass;
  }

  public function someMethod()
  {
    $this->superClass->doSomething();
  }
}

class superClass
{
  public function __construct()
  {
    $inst = new normalClass( $this );
    $inst->someMethod();
  }

  public function doSomething()
  {  //this method shall be be accessed by domeMethod form normalClass
  }
}

Or just a straight-up setter

class normalClass
{
  protected $superClass;

  public function setSuperClass( superClass $superClass )
  {
    $this->superClass = $superClass;
  }

  public function someMethod()
  {
    if ( !isset( $this->superClass ) )
    {
      throw new Exception( 'you must set a superclass' );
    }
    $this->superClass->doSomething();
  }
}

class superClass
{
  public function __construct()
  {
    $inst = new normalClass();
    $inst->setSuperClass( $this );
    $inst->someMethod();
  }

  public function doSomething()
  {  //this method shall be be accessed by domeMethod form normalClass
  }
}
Peter Bailey
A: 

Depending on your use case, you might want to pass the instance to the function only:

class normalClass {
    public function someMethod($object) { 
        $object->doSomething();
    }
}

If normalClass::someMethod() can be called by multiple, distinct $objects, this might be the better choice (instead of providing the $object to the whole normalClass instance).

But regardless of that you might consider creating an Interface to use for type hinting:

interface ISomethingDoer {
    public function doSomething();
}

class normalClass {
    public function someMethod(ISomethingDoer $object) {
        # Now PHP will generate an error if an $object is passed
        # to this function which does not implement the above interface.
        // ...

class superClass implements ISomethingDoer {
    // ...
soulmerge