Skip to content

Commit

Permalink
Updates and syntax fixes by Wilson.
Browse files Browse the repository at this point in the history
  • Loading branch information
arlina-espinoza committed Oct 18, 2017
1 parent ae2e9d4 commit 1a3bf47
Show file tree
Hide file tree
Showing 9 changed files with 52 additions and 45 deletions.
4 changes: 2 additions & 2 deletions 1.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@

# Introduction

Objects are key to understanding object-oriented technology. Look around right now and you'll find
many examples of real-world objects: your dog, your desk, your television set, your bicycle.
Objects are key to understanding object-oriented technology.You can find many examples of real-world
objects: your dog, your desk, your television set, your bicycle.

Real-world objects share two characteristics: they all have state and behavior. Dogs have state
(name, color, breed, hungry) and behavior (barking, fetching, wagging tail). Bicycles also have
Expand Down
37 changes: 21 additions & 16 deletions 10.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,20 @@
## Program to an interface, not an implementation.

We learned an *interface* is a set of methods that an object responds to, and an
*implentation* is the code and logic for the object. Generally you want to write
code you want to reference interfaces instead of implementations.
*implentation* is the code and logic for the object. Generally, when you want to write
code, you want to reference interfaces instead of implementations.

This decouples design from the specific implementation of concrete classes, and
allows you to swap out one implementation for another easily, as well as create
new implementations.

In PHP we can accomplish this by specifying interface data types in type
hinting as shown in the lesson 6 Client class example. It is important to note that by
'interface data types', we mean interfaces _or_ abstract classes.
hinting. It is important to note that by 'interface data types', we mean interfaces *or*
abstract classes.

Also, as previously noted, we can instantiate an object of a given concrete
implementation, and so long as it implements an interface or extends an
abstract class, those parent classes will be used to identify the object curing
type hinting.
implementation, and as long as it implements an interface or extends an
abstract class, it will comply with the type hinting.

## Favor object composition over class inheritance.

Expand Down Expand Up @@ -54,7 +53,8 @@ This contrived example shows a client class that uses an object.

## Strategy

Encapsulates specific families of algorithms allowing the client class responsible for instantiating a particular algorithm to have no knowledge of the actual implementation.
Encapsulating specific families of algorithms allows the client class responsible for instantiating
a particular algorithm to have no knowledge of the actual implementation.

```php
<?php
Expand Down Expand Up @@ -160,7 +160,8 @@ var_dump($anotherObj === SingletonChild::getInstance()); // bool(true)

## Factory

A factory class simply creates the object you want to use. The purpose of the factory patterns is to separate the use of a certain component, from the choice of implementation + instance management of that component.
A factory class simply creates the object you want to use. The purpose of the factory pattern is to
separate the object being created from implementation and instance management.

```php
<?php
Expand Down Expand Up @@ -198,14 +199,18 @@ print_r($veyron->getMakeAndModel()); // outputs "Bugatti Veyron"

## Dependency Injection

In Drupal 8 *dependency injection* is the preferred method for accessing and using services. Services are reusable functionality (e.g. database connection, string translation, cacheing, authentication) that are made pluggable and replaceable via dependency injection.
Drupal 8 introduces the concept of services to decouple reusable functionality and makes these
services pluggable and replaceable by registering them with a service container. As a developer,
it is best practice to access any of the services provided by Drupal via the service container to
ensure the decoupled nature of these systems is respected.

An injection is the passing of a dependency (a service) to a dependent object (a client). The service is made part of the client's state. Passing the service to the client, rather than allowing a client to build or find the service, is the fundamental requirement of the pattern.

Some of the design patterns that preceded DI:
An injection is the passing of a dependency (a service) to a dependent object (a client). The
service is made part of the client's state, either by passing them as arguments to a constructor
or injected via setter methods. Passing the service to the client, rather than allowing
a client to build or find the service, is the fundamental requirement of the pattern.

## References

- [Design Patterns - PHP: The Right Way](http://www.phptherightway.com/pages/Design-Patterns.html)
- Wikipedia
- [Dependency injection - Wikipedia, the free encyclopedia](http://en.wikipedia.org/wiki/Dependency_injection)
- [Design Patterns - PHP: The Right Way](http://www.phptherightway.com/pages/Design-Patterns.html)
- Wikipedia - [Dependency injection - Wikipedia, the free encyclopedia](http://en.wikipedia.org/wiki/Dependency_injection)
- [Services and dependency injection in Drupal 8](https://www.drupal.org/docs/8/api/services-and-dependency-injection/services-and-dependency-injection-in-drupal-8)
2 changes: 1 addition & 1 deletion 2.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
## Visibility

One interesting thing to note is that each method and property can have an associated level of
visibility, which enables information-hiding. There three types of visibility are public, private,
visibility, which enables information-hiding. The three types of visibility are public, private,
and protected.

- *Public* properties are ones that directly accessible in the instantiated object.
Expand Down
4 changes: 2 additions & 2 deletions 3.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
In the previous exercise, we created a class and instantiated an object based on the class. We then
manually set the values of properties of the object.

It is often helpful to be able to be able to declare an instance and assign values at the same time.
It is often helpful to be able to declare an instance and assign values at the same time.

Fortunately, we can do this with a special *constructor* method. In PHP the constructor method is
named `__construct`. Any method you see in object oriented PHP with two leading underscores is a
Expand Down Expand Up @@ -60,7 +60,7 @@ class Person {
We can use type hinting to ensure that arguments passed to methods (or any PHP function in general)
are of a certain type. If the given value is of the incorrect type, then an error is generated:
- In PHP 5, this will be a fatal error.
- In PHP 7 will throw a TypeError exception.
- PHP 7 will throw a TypeError exception.

```php
<?php
Expand Down
4 changes: 2 additions & 2 deletions 4.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

## Constants

Constants are variables which value cannot be changed during execution. The default visibility of
Constants are variables that cannot have their values changed during execution. The default visibility of
class constants is public. Constants can be accessed via the scope resolution operator `::`.

```php
Expand Down Expand Up @@ -64,7 +64,7 @@ test(); // 2
Declaring class properties or methods as static makes them accessible without needing an instantiation
of the class.

Attributes or methods declared as static **should not** be accessed with via an instantiated class
Attributes or methods declared as static **should not** be accessed via an instantiated class
object (`->`).

```php
Expand Down
8 changes: 4 additions & 4 deletions 5.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
- `parent`
- `protected`

Inheritance is a programming construct that software developers use to establish is-a relationships
between categories. Inheritance enables us to derive more-specific categories from more-generic
ones. The more-specific category is a kind of the more-generic category. For example, a dog is
a kind of mammal, and a bycicle is a kind of vehicle.
Inheritance is a programming construct that software developers use to establish an "is-a"
relationship between categories. Inheritance enables us to derive more-specific categories from
more-generic ones. The more-specific category is a kind of the more-generic category. For example,
a dog is a kind of mammal, and a bicycle is a kind of vehicle.

PHP supports class extension via the `extends` keyword, and specifies a parent-child relationship
between two classes
Expand Down
12 changes: 6 additions & 6 deletions 6.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ The child class *must* implement all of the abstract methods listed in the
signify an abstract property, but PHP doesn't force you to use it in the
implementing classes.

One declares an class as abstract by using the `abstract` keyword on both the
class, and any abstract methods must also use the `abstract` keyword.
A class can be declared as abstract by using the `abstract` keyword before
`class`, and any abstract methods must also use the `abstract` keyword.

Private methods cannot be declared as abstract, only public or protected
methods.
Expand All @@ -47,16 +47,16 @@ class Node extends ContentEntityBase {
}
```

Abstract classes can have have concrete methods defined. This allows for
classes that only partially need to be implemented by a derived class. In the
Abstract classes can have concrete methods defined. This allows for
classes that only *partially* need to be implemented by a derived class. In the
example above, the `id` concrete method will be available to classes that
extend the `ContentEntityBase` class.

If you fail to implement all of the abstract methods outlined in the abstract
class in a child concrete class, PHP will throw an error.

Remember that abstract methods are only declarations, and that concrete
methods must contain a function body whereas abstract methods must not.
methods must contain a function body whereas abstract methods do not.

PHP will throw an error if:

Expand Down Expand Up @@ -126,7 +126,7 @@ class Select extends Query {
}
```

##Exercise:
## Exercise:

- Create an abstract class out of the Person class from lesson 2.
- Create a concrete class that extends the abstract class.
Expand Down
11 changes: 6 additions & 5 deletions 7.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@ interface EntityInterface {

You can then implement an interface with another class by using the
`implements` keyword. Just as concrete classes can contain additional
properties and methods to augment an abstract class, so to can they augment
interfaces.
properties and methods to augment an abstract class, so can implementing classes contain more
properties/methods than declared in the interfaces; i.e., implementing classes need to comply with
the interface, and they can add any other thing they need.
Interfaces can also extend other interfaces.

```php
Expand Down Expand Up @@ -68,9 +69,9 @@ $node->set('status', NodeInterface::PUBLISHED);

```

### instanceOf
### `instanceOf`

The instanceof operator is used to find out if an object is an instantiated instance of a class. This
The `instanceof` operator is used to find out if an object is an instantiated instance of a class. This
can be useful to make sure that a parameter or variable is a particular instance of an object before
using it. It is particularly useful to check against an interface, which is more generic than an
implementing class.
Expand All @@ -88,7 +89,7 @@ function hook_entity_presave(EntityInterface $entity) {
```

### Drupal Examples:
Entities are a great place to look at interfaces. Interface allows for a developer wishing
Entities are a great place to look at interfaces. An interface allows developers wishing
to create a custom entity to do so simply by adhering to the interface.

Drupal's content entities (`NodeInterface`, '`CommentInterface`, `UserInterface`, etc.) extend the
Expand Down
15 changes: 8 additions & 7 deletions 9.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

## Namespaces

Namespaces are a way to encapsulate code, such that to access it, you must first declare to which namespace you are
Namespaces are a way to encapsulate code, such that to access it, you must first declare which namespace you are
referring. In practical terms, what this means is that adding a namespace to a class is very much like organizing
classes into hierarchical directories.

Expand All @@ -26,8 +26,8 @@ class Person {

Note: Namespaces can be applied to classes, interfaces, and other similar structures as well.

To reference a namespaced class, for example:
`Symfony\Component\Yaml\Yaml`
To reference a namespaced class (for example, the class `Symfony\Component\Yaml\Yaml`), you can
reference it by it's fully qualified name:

```php
<?php
Expand All @@ -42,7 +42,7 @@ class Person {
}
```

Or:
Or by declaring the `use` keyword and adding the namespace to the top of the class:

```php
<?php
Expand Down Expand Up @@ -73,9 +73,10 @@ class Person {
}
}
```
##Autoloading

Once classes have been organized into namespaces, PHP can take advantage to autoload them whenever needed.
## Autoloading

Once classes have been organized into namespaces, PHP can take advantage and autoload them whenever needed.
PSR-4 describes a specification for autoloading classes from file paths, and is used by popular projects such as
Symfony and Composer.

Expand All @@ -97,7 +98,7 @@ modules/chapterthree/
```


##Exercise:
## Exercise:

- Add a namespace to the top of your file, this will put all classes defined in that file into that namespace.
- Add a constant or static variable to one of your classes / interfaces.
Expand Down

0 comments on commit 1a3bf47

Please sign in to comment.