As promised, a code we can discuss about.
The short story… picture-short: http://i1054.photobucket.com/albums/s490/itmitica/itmitica-dog-2.png
The short story… php-short:
// A short story
echo '<h1>World events</h1>';
// Once upon a time on Earth.
$earth = new World( 'Earth' );
// Something was happening.
echo '<hr>';
echo '<h2>Event #1</h2>';
// A dog named Spot was barking at John's house.
// John, like a good master, feeds Spot.
// Spot is eating now.
$spot = new Dog( 'Spot' );
$spot -> bark();
$john = new Man( 'John' );
$john_house = new Location( 'house', $john );
$spot_barking = new Sound( $spot, $john_house );
$earth -> event( $spot_barking );
// Another thing happen then.
echo '<hr>';
echo '<h2>Event #2</h2>';
// A dog named Rex sees Spot eating and barks at John's house too.
// John is not Rex's master. He's chasing Rex away.
$rex = new Dog( 'Rex' );
$rex -> bark();
$rex_barking = new Sound( $rex, $john_house );
$earth -> event( $rex_barking );
// Rex runs to his master's house, Bob, and barks.
// Bob is also a good master. He feeds Rex.
// Rex is eating too now.
echo '<hr>';
echo '<h2>Event #3</h2>';
$bob = new Man( 'Bob' );
$bob_house = new Location( 'house', $bob );
$rex_barking -> changeLocation( $bob_house );
$rex -> bark();
$earth -> event( $rex_barking );
// The End.
echo '<hr>';
The whole deal in one file:
<?php
trait Base {
public function getProperty( $property ) {
return $this -> $property;
}
}
class World {
use Base;
protected $name;
protected $event;
public function __construct ( $name ) {
$this -> name = $name;
}
public function event( $event ) {
$this -> event = $event;
echo '<h3>' .
$this -> event -> getProperty( 'source' ) -> getProperty( 'name' ) . ' is ' .
$this -> event -> getProperty( 'source' ) -> getProperty( 'activity' ) . ' at ' .
$this -> event -> getProperty( 'location' ) -> getProperty( 'owner' ) -> getProperty ( 'name' ) . '\\'s ' .
$this -> event -> getProperty( 'location' ) -> getProperty( 'type' ) . '.' .
'</h3>';
if ( $this -> event -> getProperty( 'location' ) -> getProperty( 'type' ) == 'house' ) {
$this -> event -> getProperty( 'location' ) -> getProperty( 'owner' ) ->
alert( $this -> event -> getProperty( 'source' ) );
}
echo '<p>' .
$this -> event -> getProperty( 'location' ) -> getProperty( 'owner' ) -> getProperty ( 'name' ) . ' is ' .
$this -> event -> getProperty( 'location' ) -> getProperty( 'owner' ) -> getProperty ( 'activity' ) . ' ' .
$this -> event -> getProperty( 'source' ) -> getProperty( 'name' ) . '. ' .
$this -> event -> getProperty( 'source' ) -> getProperty( 'name' ) . ' is ' .
$this -> event -> getProperty( 'source' ) -> getProperty( 'activity' ) . '.' .
'</p>';
}
}
class Sound {
use Base;
protected $source;
protected $location;
public function __construct( $source, $location ) {
$this -> source = $source;
$this -> location = $location;
}
public function changeLocation( $newlocation ) {
$this -> location = $newlocation;
}
}
class Location {
use Base;
protected $type;
protected $owner;
public function __construct( $type, $owner ) {
$this -> type = $type;
$this -> owner = $owner;
}
}
class Man {
use Base;
protected $name;
protected $activity = 'idle';
protected $alertsource;
protected $dogResponse = [
'feeding' => 'eat',
'chasing' => 'run'
];
public function __construct( $name ) {
$this -> name = $name;
}
public function alert( $source ) {
$this -> alertsource = $source;
switch( $this -> alertsource -> getProperty( 'activity' ) ) {
case 'barking':
$this -> handleDog();
}
}
protected function handleDog () {
if ( $this -> alertsource -> getProperty( 'master' ) == $this -> name ) {
$this -> activity = 'feeding';
} else {
$this -> activity = 'chasing';
};
call_user_func( [ $this -> alertsource, $this -> dogResponse[ $this -> activity ] ] );
}
}
class Dog {
use Base;
protected $name;
protected $master;
protected $activity = 'idle';
protected $dogMaster = [
'Spot' => 'John',
'Rex' => 'Bob'
];
public function __construct ( $name ) {
$this -> name = $name;
$this -> master = $this -> dogMaster[ $this -> name ];
}
public function bark() {
$this -> activity = 'barking';
}
public function eat() {
$this -> activity = 'eating';
}
public function run() {
$this -> activity = 'running';
}
}
// A short story
echo '<h1>World events</h1>';
// Once upon a time on Earth.
$earth = new World( 'Earth' );
// Something was happening.
echo '<hr>';
echo '<h2>Event #1</h2>';
// A dog named Spot was barking at John's house.
// John, like a good master, feeds Spot.
// Spot is eating now.
$spot = new Dog( 'Spot' );
$spot -> bark();
$john = new Man( 'John' );
$john_house = new Location( 'house', $john );
$spot_barking = new Sound( $spot, $john_house );
$earth -> event( $spot_barking );
// Another thing happen then.
echo '<hr>';
echo '<h2>Event #2</h2>';
// A dog named Rex sees Spot eating and barks at John's house too.
// John is not Rex's master. He's chasing Rex away.
$rex = new Dog( 'Rex' );
$rex -> bark();
$rex_barking = new Sound( $rex, $john_house );
$earth -> event( $rex_barking );
// Rex runs to his master's house, Bob, and barks.
// Bob is also a good master. He feeds Rex.
// Rex is eating too now.
echo '<hr>';
echo '<h2>Event #3</h2>';
$bob = new Man( 'Bob' );
$bob_house = new Location( 'house', $bob );
$rex_barking -> changeLocation( $bob_house );
$rex -> bark();
$earth -> event( $rex_barking );
// The End.
echo '<hr>';
?>
You’ll need PHP 5.4 for it. I’ve added a trait, just for fun, and I’ve used shorthand for arrays. You can change the Base trait to Base class, extend the classes, remove use and replace the shorthand for arrays with full 5.3 declaration.
<hr>
The base of the story is this: all objects are brought back together in a world. A sound is happening at a location, but it can change its location. A man responds to an event by identifying the source and taking appropriate action. A source (dog) will react to the action of the man.
The parallel would be web requests. The web server is a world. A sound is a request by a user (a dog). A man is the app delegated to handle the request and to send back a response to the user.
<hr>
The result is something like this:
World events
Event #1
Spot is barking at John's house.
John is feeding Spot. Spot is eating.
Event #2
Rex is barking at John's house.
John is chasing Rex. Rex is running.
Event #3
Rex is barking at Bob's house.
Bob is feeding Rex. Rex is eating.
Feel free to ask a question or dismantle the model. I won’t mind, I promise.