-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Updates to code samples and exercises.
- Loading branch information
1 parent
1a3bf47
commit d5d0a81
Showing
14 changed files
with
311 additions
and
82 deletions.
There are no files selected for viewing
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
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
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
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,22 @@ | ||
<?php | ||
|
||
function cat() { | ||
$cat = new stdClass(); | ||
$cat->name = 'frank'; | ||
$cat->meow = function() { | ||
echo 'meow'; | ||
}; | ||
return $cat; | ||
class Person { | ||
|
||
public $name; | ||
public $age; | ||
|
||
function getName() { | ||
return $this->name; | ||
} | ||
|
||
function getAge() { | ||
return $this->age; | ||
} | ||
|
||
} | ||
|
||
$cat1 = cat(); | ||
unset($cat1->name); | ||
var_dump($cat1->name); | ||
call_user_func($cat1->meow); | ||
$person = new Person(); | ||
$person->name = 'Arlina'; | ||
$person->age = 32; | ||
|
||
var_dump($person); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,40 @@ | ||
<?php | ||
|
||
class Person { | ||
private $name; | ||
|
||
function __construct($name) { | ||
$this->name = $name; | ||
} | ||
private $name; | ||
private $age; | ||
private $height; | ||
|
||
function setName($name) { | ||
public function setName($name) { | ||
$this->name = $name; | ||
} | ||
|
||
public function getName() { | ||
return $this->name; | ||
} | ||
|
||
public function setAge($age) { | ||
$this->age = $age; | ||
} | ||
|
||
public function getAge() { | ||
return $this->age; | ||
} | ||
|
||
public function setHeight($height) { | ||
$this->height = $height; | ||
} | ||
|
||
public function getHeight() { | ||
return $this->height; | ||
} | ||
|
||
} | ||
|
||
$person = new Person('asdf'); | ||
$person->setName('ryryr'); | ||
var_dump($person->getName()); | ||
$person = new Person(); | ||
$person->setName('Arlina'); | ||
$person->setAge(32); | ||
$person->setHeight(160); | ||
|
||
var_dump($person); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,45 @@ | ||
<?php | ||
|
||
function Person() { | ||
$person = new stdClass(); | ||
$person->name = 'frank'; | ||
$person->talk = function() { | ||
echo 'hi'; | ||
}; | ||
return $person; | ||
class Person { | ||
|
||
private $name; | ||
private $age; | ||
private $height; | ||
private $birth_year; | ||
|
||
public function __construct(string $name = 'Anonymous', int $age = NULL, float $height = NULL) { | ||
$this->name = $name; | ||
$this->age = $age; | ||
$this->height = $height; | ||
$this->birth_year = date('Y') - $this->age; | ||
} | ||
|
||
public function setName(string $name) { | ||
$this->name = $name; | ||
} | ||
|
||
public function getName() { | ||
return $this->name; | ||
} | ||
|
||
public function setAge(int $age) { | ||
$this->age = $age; | ||
} | ||
|
||
public function getAge() { | ||
return $this->age; | ||
} | ||
|
||
public function setHeight(float $height) { | ||
$this->height = $height; | ||
} | ||
|
||
public function getHeight() { | ||
return $this->height; | ||
} | ||
|
||
} | ||
|
||
$person = Person(); | ||
var_dump(is_a($person, 'stdClass')); | ||
$person = new Person('Arlina', 32, 160); | ||
|
||
var_dump($person); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
<?php | ||
|
||
class Person { | ||
|
||
private $name; | ||
private $age; | ||
|
||
public function __construct(string $name = 'Anonymous', int $age = NULL) { | ||
$this->name = $name; | ||
$this->age = $age; | ||
} | ||
|
||
public function setName(string $name) { | ||
$this->name = $name; | ||
} | ||
|
||
public function getName() { | ||
return $this->name; | ||
} | ||
|
||
public function setAge(int $age) { | ||
$this->age = $age; | ||
} | ||
|
||
public function getAge() { | ||
return $this->age; | ||
} | ||
|
||
/** | ||
* Return the person who is older, or NULL if they have the same age. | ||
* | ||
* @param \Person $a | ||
* @param \Person $b | ||
* | ||
* @return null|\Person | ||
*/ | ||
public static function returnWhoIsOlder(Person $a, Person $b) { | ||
if ($a->getAge() > $b->getAge()) { | ||
return $a; | ||
} | ||
elseif ($b->getAge() > $a->getAge()) { | ||
return $b; | ||
} | ||
|
||
return NULL; | ||
} | ||
|
||
} | ||
|
||
$personA = new Person('Arlina', 32); | ||
$personB = new Person('Methuselah', 969); | ||
$oldest = Person::returnWhoIsOlder($personA, $personB); | ||
echo $oldest->getName() . ' is older'; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,51 @@ | ||
<?php | ||
|
||
abstract class APerson { | ||
abstract function getName(); | ||
abstract function setName($name); | ||
class Person { | ||
private $name; | ||
|
||
public function getName() { | ||
return $this->name; | ||
} | ||
|
||
public function setName(string $name) { | ||
$this->name = $name; | ||
} | ||
|
||
public function sayHi() { | ||
echo "hi" . PHP_EOL; | ||
echo "Hi"; | ||
} | ||
} | ||
|
||
class CPerson extends APerson { | ||
private $name; | ||
class Developer extends Person { | ||
|
||
function getName() { | ||
return $this->name; | ||
public function work() { | ||
echo 'Coding!' . PHP_EOL; | ||
} | ||
|
||
function __construct() { | ||
$this->sayHi(); | ||
public function sayHi() { | ||
parent::sayHi(); | ||
echo "I'm a developer." . PHP_EOL; | ||
} | ||
|
||
function setName($name) { | ||
$this->name = $name; | ||
} | ||
|
||
class Designer extends Person { | ||
|
||
public function work() { | ||
echo 'Designing!' . PHP_EOL; | ||
} | ||
|
||
public function sayHi() { | ||
parent::sayHi(); | ||
echo "I'm a designer." . PHP_EOL; | ||
} | ||
|
||
} | ||
|
||
$person = new CPerson(); | ||
$person->setName('Alice'); | ||
echo $person->getName(); | ||
$programmer = new Developer(); | ||
$programmer->sayHi(); // Hi I'm a developer. | ||
$programmer->work(); // Coding! | ||
|
||
$designer = new Designer(); | ||
$designer->sayHi(); // Hi I'm a designer. | ||
$designer->work(); // Designing! |
Oops, something went wrong.