Skip to content

Commit

Permalink
Updates to code samples and exercises.
Browse files Browse the repository at this point in the history
  • Loading branch information
arlina-espinoza committed Oct 18, 2017
1 parent 1a3bf47 commit d5d0a81
Show file tree
Hide file tree
Showing 14 changed files with 311 additions and 82 deletions.
3 changes: 2 additions & 1 deletion 1.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,8 @@ $person->sayName(); //Joe Bob

##### Notes:

Objects are always passed by reference (rather than making a copy of it), meaning that a function that accepts an object as a parameter can modify the original object passed to it.
Objects are always passed by reference (rather than making a copy of it), meaning that a function
that accepts an object as a parameter can modify the original object passed to it.

## Coding Standards:

Expand Down
8 changes: 5 additions & 3 deletions 3.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Lesson 3: Constructors
# Lesson 3: Constructors and Type Hinting

## New Keywords:
- `__construct`
Expand Down Expand Up @@ -81,7 +81,9 @@ class Person {

### Exercise:

1 Using the class from Exercise 2, create a constructor function that takes
1 Using the class from Lesson 2, create a constructor function that takes
a $name parameter and assigns that value to the name property.

2. Add 'height' and 'haircolor' to your constructor.
2. Add 'height' and 'height' to your constructor.

3. Make use of type hints in the arguments to the methods.
1 change: 1 addition & 0 deletions 4.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,3 +90,4 @@ $url = Url::fromUserInput('/robots.txt');
### Exercise:

1. Add a static method to the "Person" class that compares two individuals by age and returns the oldest one.
2. Call that static method.
9 changes: 8 additions & 1 deletion 5.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class Node extends ContentEntityBase {

```

###Protected: visibility
### Protected: visibility

Another level of visibility (information hiding) is "protected", which means that a protected property or method can be
accessed only from within the class defining it, or any subclassses. In contrast, a private property/method is only
Expand Down Expand Up @@ -100,3 +100,10 @@ echo $b->private; // Fatal error
$b->testVisibilityA(); // Public, Protected, Private
$b->testVisibilityB(); // Fatal error: subclass can't access $this->private
```

## Exercises:

- Add a method "sayHi()" to the Person class.
- Create two sub-classes "Developer" and "Designer" that extend the Person class.
- Add a method "work()" to each of those subclasses.
- Use "parent" in a sub-class method.
14 changes: 10 additions & 4 deletions 6.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,15 @@ class Select extends Query {
}
```

## `final`

The `final` keyword prevents child classes from overriding a method by prefixing the definition
with final. If the class itself is being defined final then it cannot be extended.

## Exercise:

- Create an abstract class out of the Person class from lesson 2.
- Create a concrete class that extends the abstract class.
- Instantiate an object from the concrete class.
- Set the name of the person object, and then echo it back out.
- Convert the Person class into an abstract class.
- Convert into concrete classes the Developer and Designer classes.
- Instantiate objects from the concrete classes.
- Call some methods on those objects to see how they behave.
- Try instantiating an object of the abstract class to look at the PHP error.
9 changes: 5 additions & 4 deletions 7.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,8 @@ and provide a consistent API for module developers.

## Exercise:

- Create an interface to give a Person object from lesson 3 an email address and website.
- Modify the concrete class from lesson 3 to implement your new interface.
- Instantiate an object from the concrete class.
- Set the email and website of the object, and then echo it back out.
- Create a person interface that declares the methods "work()" and "sayHi()".
- Modify the abstract class Person so it implements that interface.
- Instantiate an object from a concrete class.
- Review what errors does PHP give when a method declared in the interface is missing on the
implementing class.
13 changes: 7 additions & 6 deletions 9.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ namespace ChapterThree\OOTraining;

class Person {
public function __construct($filename) {
$array = \Symfony\Component\Yaml\Yaml::parse(file_get_contents($filename));
$array = \Symfony\Component\Yaml::parse(file_get_contents($filename));
// ... Do something else.
}
}
Expand All @@ -49,7 +49,7 @@ Or by declaring the `use` keyword and adding the namespace to the top of the cla

namespace ChapterThree\OOTraining;

use Symfony\Component\Yaml\Yaml;
use Symfony\Component\Yaml;

class Person {
public function __construct($filename) {
Expand All @@ -58,18 +58,20 @@ class Person {
}
}
```
### Use as
### Use `as`

You can also create *aliases* to refer to the classes using `as`:

```php
<?php

namespace ChapterThree\OOTraining;

use Symfony\Component\HttpKernel as kernel;
use Symfony\Component\HttpKernel as Kernel;

class Person {
public function __construct($filename) {
kernel::terminate();
Kernel::terminate();
}
}
```
Expand Down Expand Up @@ -97,7 +99,6 @@ modules/chapterthree/
chapterthree.module
```


## Exercise:

- Add a namespace to the top of your file, this will put all classes defined in that file into that namespace.
Expand Down
29 changes: 18 additions & 11 deletions code_samples/1.php
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);
34 changes: 26 additions & 8 deletions code_samples/2.php
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);
50 changes: 41 additions & 9 deletions code_samples/3.php
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);
53 changes: 53 additions & 0 deletions code_samples/4.php
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';
51 changes: 36 additions & 15 deletions code_samples/5.php
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!
Loading

0 comments on commit d5d0a81

Please sign in to comment.