mirror of
https://github.com/codeguy/php-the-right-way.git
synced 2025-08-12 00:33:58 +02:00
Merge branch 'gh-pages' of https://github.com/codeguy/php-the-right-way into gh-pages
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -1 +1,2 @@
|
||||
/_site/
|
||||
*.DS_Store
|
||||
|
18
LICENSE
18
LICENSE
@@ -1,19 +1,3 @@
|
||||
Copyright (c) 2012 Josh Lockhart
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is furnished
|
||||
to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
http://creativecommons.org/licenses/by-nc-sa/3.0/
|
||||
|
@@ -34,6 +34,6 @@ There's been a lot of discussion lately about how the PHP community lacks suffic
|
||||
|
||||
My name is [Josh Lockhart](http://twitter.com/codeguy). I'm the author of the [Slim Framework](http://www.slimframework.com/), and I work for [New Media Campaigns](http://www.newmediacampaigns.com/).
|
||||
|
||||
## Copyright
|
||||
## License
|
||||
|
||||
[MIT](http://opensource.org/licenses/MIT)
|
||||
[Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License][http://creativecommons.org/licenses/by-nc-sa/3.0/]
|
||||
|
@@ -1,6 +1,6 @@
|
||||
# Welcome
|
||||
|
||||
There's a lot of bad information on the Web (I'm looking at you, W3Schools) that leads new PHP users astray, propagating bad practices and bad code. This must stop. _PHP: The Right Way_ is an easy-to-read, quick reference for PHP best practices, accepted coding standards, and links to authoritative tutorials around the Web.
|
||||
There's a lot of outdated information on the Web that leads new PHP users astray, propagating bad practices and bad code. This must stop. _PHP: The Right Way_ is an easy-to-read, quick reference for PHP best practices, accepted coding standards, and links to authoritative tutorials around the Web.
|
||||
|
||||
It is important to understand _there is no canonical way to use PHP_. That's the beauty of it. This website introduces new PHP developers to best practices, available options, and good information.
|
||||
|
||||
|
@@ -62,9 +62,13 @@
|
||||
</ul>
|
||||
|
||||
<h2 class="epsilon">Project sponsors</h2>
|
||||
<ul>
|
||||
<ul class="mbd">
|
||||
<li><a href="http://www.newmediacampaigns.com">New Media Campaigns</a></li>
|
||||
</ul>
|
||||
|
||||
<p>
|
||||
<a rel="license" href="http://creativecommons.org/licenses/by-nc-sa/3.0/"><img alt="Creative Commons License" style="border-width:0" src="http://i.creativecommons.org/l/by-nc-sa/3.0/88x31.png" /></a><br /><span xmlns:dct="http://purl.org/dc/terms/" href="http://purl.org/dc/dcmitype/Text" property="dct:title" rel="dct:type">PHP: The Right Way</span> by <a xmlns:cc="http://creativecommons.org/ns#" href="http://www.twitter.com/codeguy" property="cc:attributionName" rel="cc:attributionURL">Josh Lockhart</a> is licensed under a <a rel="license" href="http://creativecommons.org/licenses/by-nc-sa/3.0/">Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License</a>.<br />Based on a work at <a xmlns:dct="http://purl.org/dc/terms/" href="http://www.phptherightway.com" rel="dct:source">www.phptherightway.com</a>.
|
||||
</p>
|
||||
</footer>
|
||||
</div>
|
||||
|
||||
|
1
_posts/05-01-01-Coding-Practices.md
Normal file
1
_posts/05-01-01-Coding-Practices.md
Normal file
@@ -0,0 +1 @@
|
||||
# Coding Practices
|
@@ -1,6 +1,9 @@
|
||||
# Databases and PDO
|
||||
|
||||
Many times your PHP code will use a database to persist information. If you use a database, use `PDO` to talk with it. PDO is a database abstraction library — (usually) built into PHP — that provides a common interface to talk with many different databases.
|
||||
Many times your PHP code will use a database to persist information. If you use a database, use `PDO` to talk with it. PDO is a
|
||||
database connection abstraction library — built into PHP since 5.1.0 — that provides a common interface to talk with
|
||||
many different databases. PDO will not translate your SQL queries or emulate missing features, it is purely for connecting to multiple
|
||||
types of database with the same API.
|
||||
|
||||
More importantly, `PDO` allows you to safely inject foreign input (e.g. IDs) into your SQL queries without worrying about database SQL injection attacks. This is possible using PDOStatements and bound parameters.
|
||||
|
||||
@@ -12,7 +15,8 @@ $pdo = new PDO('sqlite:users.db');
|
||||
$pdo->query("SELECT name FROM users WHERE id = " . $_GET['id']); // <-- NO!
|
||||
{% endhighlight %}
|
||||
|
||||
This is terrible code. You are inserting a raw query parameter into a SQL query. This will get you hacked in a heartbeat. Instead, you should sanitize the ID input using PDO bound parameters.
|
||||
This is terrible code. You are inserting a raw query parameter into a SQL query. This will get you hacked in a heartbeat. Instead,
|
||||
you should sanitize the ID input using PDO bound parameters.
|
||||
|
||||
{% highlight php %}
|
||||
<?php
|
||||
@@ -22,9 +26,20 @@ $stmt->bindParam(':id', filter_input(INPUT_GET, 'id', FILTER_SANITIZE_NUMBER_INT
|
||||
$stmt->execute();
|
||||
{% endhighlight %}
|
||||
|
||||
This is correct code. It uses a bound parameter on a PDO statement. This escapes the foreign input ID before it is introduced to the database preventing potential SQL injection attacks.
|
||||
This is correct code. It uses a bound parameter on a PDO statement. This escapes the foreign input ID before it is introduced to the
|
||||
database preventing potential SQL injection attacks.
|
||||
|
||||
* [Learn about PDO][1]
|
||||
|
||||
## Abstraction Layers
|
||||
|
||||
Many frameworks provide their own abstraction layer which may or may not sit on top of PDO. These will often emulate features for
|
||||
one database system that another is missing form another by wrapping your queries in PHP methods, giving you actual database abstraction.
|
||||
This will of course add a little overhead, but if you are building a portable application that needs to work with MySQL, PostgreSQL and
|
||||
SQLite then a little overhead will be worth it the sake of code cleanliness.
|
||||
|
||||
Some abstraction layers have been built using the PSR-0 namespace standard so can be installed in any application you like:
|
||||
|
||||
* [Doctrine2 DBAL][2]
|
||||
* [ZF2 Db][4]
|
||||
* [ZF1 Db][3]
|
@@ -2,7 +2,7 @@
|
||||
isChild: true
|
||||
---
|
||||
|
||||
## Behaviour Driven Development
|
||||
## Behavior Driven Development
|
||||
|
||||
There are two different types of Behavior-Driven Development (BDD): SpecBDD and StoryBDD. SpecBDD focuses on technical behavior or code, while StoryBDD focuses on business or feature behaviors or interactions. PHP has frameworks for both types of BDD.
|
||||
|
Reference in New Issue
Block a user