Last active
September 30, 2015 03:50
-
-
Save puiutucutu/735013997bffa312781a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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); | |
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
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 gettergetMovieName()
in theMovie
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...