Skip to content

Instantly share code, notes, and snippets.

@klhall1987
Last active October 29, 2015 13:03
Show Gist options
  • Save klhall1987/93f23c527ebb845af55e to your computer and use it in GitHub Desktop.
Save klhall1987/93f23c527ebb845af55e to your computer and use it in GitHub Desktop.
Method Chaining with phones
<?php
class Phone
{
//Creating private properties
private $_type;
private $_os;
public function setType( $type )
{
//setting private property equal to the $type variable.
$this->_type = $type;
//returning the private property that has been set to the $type variable.
return $this;
}
public function setOS( $os )
{
//setting private property equal to the $os variable.
$this->_os = $os;
//returning the private property that has been set to the $os variable.
return $this;
}
public function getPhone()
{
//returns a sting with the properties being called
return 'I have a(n) ' . $this->_type . ' running ' . $this->_os . '.';
}
}
$phone = new Phone();
//returns:"I have a(n) HTC One M8 running Android Lollipop."
echo $phone->setType('HTC One M8')->setOS('Android Lollipop')->getPhone();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment