Skip to content

Instantly share code, notes, and snippets.

@klhall1987
Last active December 30, 2015 20:41
Show Gist options
  • Save klhall1987/65d8edb5e6b6430b0ee6 to your computer and use it in GitHub Desktop.
Save klhall1987/65d8edb5e6b6430b0ee6 to your computer and use it in GitHub Desktop.
<?php
class user
{
/**
* @var
*/
private $userPhone;
/**
* @var string
*/
private $userName = '';
/**
* user constructor.
* @param $userName
* @param phone $userPhone
*
* Using the same phone object from the previous example we have injected an instance of the phone object
* into the constructor. Now when we instantiate this object later we will need to pass in an instance of the phone
* object. This means we no longer need to hard code the params from the phone object into this constructor.
*/
public function __construct( $userName, phone $userPhone )
{
$this->userName = $userName;
$this->userPhone = $userPhone;
}
public function getUserName()
{
return $this->userName;
}
public function getUserPhone()
{
return $this->userPhone;
}
}
$phone = new phone( 'HTC', 'One M8' );
$user = new user( 'Kenny', $phone );
echo '<pre>' . '<br>';
print_r( $user );
echo '<pre>';
****************************OUTPUT**********************************
user Object
(
[userPhone:user:private] => phone Object
(
[brand:phone:private] => HTC
[model:phone:private] => One M8
)
[userName:user:private] => Kenny
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment