Created
February 17, 2014 12:17
-
-
Save emohamed/9049564 to your computer and use it in GitHub Desktop.
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
# PHP OOP Learning Plan | |
## Lecture 1 - Basic Concepts | |
* Classes describe real-world objects or provide useful abstractions. Setup good example without any code -- e.g. `Widget` class | |
* Object are instances of a class. They are actual entities that programmers use to get the job done | |
* PHP syntax -- `class` and `new` keywords. _Live demo._ | |
* `__construct()` special method. It can't return a value since it automatically returns the newly created object. _Live demo._ | |
* Object properties. Describe the `->` syntax. _Live demo._ | |
* Object methods. _Live demo._ | |
* `$this` keyword in class definition. _Live demo._ | |
* `public` and `private` properties. _Live demo._ | |
* Class vs Object data: static properties and methods | |
* `static` and `self::` keywords | |
* constants | |
* _Live demo_: widget instance counter | |
* _Live demo_: Creating single instance of a class (example: Logger) | |
## Lecture 2 - Inheritance and exceptions | |
* inherirance provides a mechanism to base a class on another class. It's a powerful tool for code reuse | |
* The aim of inheritance is to reduce the complexity of the code. Do not use it merely to avoid code duplication. | |
* extends keyword. | |
* Protected properties | |
* parent:: keyword | |
* _Live demo._: Create an HTML widget class that renders the text without `nl2br()` | |
* inheritance may be dangerous when used incorrectly. Follow the rule: "A is a B" rather than "A has B". I.e. `WidgetHTML` is a Widget, however, Widget has a `WidgetSection`, therefore `WidgetSection` must not extend from `Widget` class. | |
* Good inheritance is easy to understand and modify. If you're not absolutly sure that inheritance "feels" right for the problem you're dealing with, prefer alternative solutions(e.g. object composition). | |
* Exceptions basic concepts. Exception are errors or exceptional events that happen at some point of a script lifetime. The reason they exist: | |
function find_users() { | |
$db_connection_status = connect_to_database(); | |
if ($db_connection_status === DB_ERROR_BAD_HOST) { | |
Logger::log("Couldn't connect to database: Bad Host. "); | |
return null; | |
} else if ($db_connection_status === DB_ERROR_INVALID_CREDENTIALS) { | |
Logger::log("Couldn't connect to database: insufficient username and password. "); | |
return null; | |
} else if ($db_connection_status === DB_ERROR_TIMEOUT) { | |
Logger::log("Couldn't connect to database: database has gown away. "); | |
return null; | |
} | |
// database connection is estabilished correctly ... | |
return perform_db_query("SELECT * FROM `users`"); | |
} | |
* Exceptions throwing and catching mechanism. | |
function find_users() { | |
try { | |
$db_connection_status = connect_to_database(); | |
} catch(Exception $e) { | |
Logger::log($e->getMessage()); | |
return null; | |
} | |
// database connection is estabilished correctly ... | |
return perform_db_query("SELECT * FROM `users`"); | |
} | |
/* ... */ | |
function connect_to_database() { | |
$res = mysql_connect(DB_HOST, DB_USER, DB_PASS); | |
if (!$res) { | |
throw new Exception(mysql_error()); | |
} | |
... | |
} | |
* Exceptions break the call stack _live demo_. They shouldn't be used to control the usual code flow. | |
route_request(); // exception will reach this line, unless catched earlier | |
function route_request() { | |
$users = get_users(); | |
/* ... */ | |
} | |
function get_users() { | |
connect_to_db(); | |
/* ... */ | |
} | |
function connect_to_db() { | |
$res = mysql_connect(DB_HOST, DB_USER, DB_PASS); | |
if (!$res) { | |
throw new Exception(mysql_error()); | |
} | |
} | |
* All exceptions are instances of the `Exception` class(or some of it's subclasses). Therefore every exception has getMessage() and other common methods: http://www.php.net/manual/en/class.exception.php | |
* Exception classes hierarchy provides a good example for inherirance usage. | |
* |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment