Skip to content

Instantly share code, notes, and snippets.

@puiutucutu
Last active September 30, 2015 03:50
Show Gist options
  • Save puiutucutu/735013997bffa312781a to your computer and use it in GitHub Desktop.
Save puiutucutu/735013997bffa312781a to your computer and use it in GitHub Desktop.
<?php
class MovieTheater {
private $objectBoxOffice;
private $objectMovie;
public function __construct(BoxOffice $boxOffice)
{
// using composition
$this->objectBoxOffice = $boxOffice;
// using aggregation
$this->objectMovie = new Movie;
}
}
class BoxOffice {
private $register;
public function ticketDispenser() { /* */ }
public function cashierMachine() { /* */ }
}
Class Movie {
private $movie_name = 'Conan';
private $rating;
private $duration;
public function playMovie() { /* */ }
}
// create a box office object
$boxOffice = new BoxOffice;
// create a movie theater object, passing the BoxOffice object as a property
$movieTheater = new MovieTheater($boxOffice);
// debugging
echo '<pre><code>';
print_r($movieTheater);
@puiutucutu
Copy link
Author

Notes

Modelling the UML diagram below into code. Only problem is that the private properties inside the Movie class are not accessible by the MovieTheater class. However, if we create a getter getMovieName() in the Movie class, then we could do $this->objectMovie->getMovieName(); and access the movie title.

Doing all this though, in my mind, seems to defeat the entire purpose of a private property. Not sure what conclusion to draw yet...

image

@puiutucutu
Copy link
Author

Print Array Dump

PHP considers both objects the same, what is different however is the way that the objects were instantiated - one through composition and the other through aggregation.

MovieTheater Object
(
    [objectBoxOffice:MovieTheater:private] => BoxOffice Object
        (
            [register:BoxOffice:private] => 
        )

    [objectMovie:MovieTheater:private] => Movie Object
        (
            [movie_name:Movie:private] => Conan
            [rating:Movie:private] => 
            [duration:Movie:private] => 
        )

)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment