Jump to content

How to communicate back and forwards between classes?


UrbanDweller
 Share

Recommended Posts

Ive always had a bit of trouble with php and effectively talking between classes,  as the example below shows I want to call a first class->function that will later call a different class and its function then call the previous class again. I havent been able to find any examples that state how to do this if possible.

 

<?php
class A{
function b(){
	//does required code etc...
	$B = new B();
	$B->BA():
}

function AB(){
	echo "I WANT TO END UP HERE, from class B function BA";	
}
}

class B{
function BA(){
	//does required code etc...
	//		
	//Unsure how to call class/function A->AB
}

}

$a = new A();
$a->AB();
?>

 

Is this possible as it helps separate my code in to nice and easy to read

 

Thanks

Link to comment
Share on other sites

Why is it going from A to B and back to A again? Seems weird.

 

Pass A to B's method.

$B = new B();
$B->BA($this);

function BA(A $a) {
    $a->AB();
}

 

Yeah it is but how I started coding this, I may have to reconsider moving some functions out of class a into class b instead as the variable pass through will get messier than i want it.

Link to comment
Share on other sites

What you're looking for is an Observer Pattern or Closures, in order to implement callbacks in an agnostic manner in a class.

 

Observer: class A should maintain a list of observers (B can register with A), and update them on Event.

Closure:  you can use anonymous functions / closures to bind a callback to B from A (requires PHP 5.3).

 

Here's an example of anonymous functions used in PHP for callbacks (you need to specify a context because binding isn't avail in PHP yet):

 

<?php

class A {
    private $callback;
    private $callbackContext;
    public function __construct($callback=null, $callbackContext=null) {
        $this->callback = $callback;
        $this->callbackContext = $callbackContext;
    }
    public function foo() {
        echo "A just fooed!<br />\n";
        if ($this->callback instanceof Closure) {
            $callback =& $this->callback;
            $callback($this->callbackContext);
        }
    }
}

class B {
    private $a;
    public function __construct() {
        $this->a = new A(function($self) {
            $self->bar();
        }, $this);
    }
    public function foo() {
        echo "B just fooed!<br />\n";
        $this->a->foo();
    }
    public function bar() {
        echo "B just barred!<br />\n";
    }
}

$b = new B();
$b->foo();

?>

Link to comment
Share on other sites

This thread is more than a year old. Are you sure you have something important to add to it?

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.