mirror of
https://github.com/codeguy/php-the-right-way.git
synced 2025-08-08 06:56:33 +02:00
Merge branch 'gh-pages' of github.com:codeguy/php-the-right-way into gh-pages
This commit is contained in:
@@ -1,7 +1,4 @@
|
|||||||
auto: true
|
|
||||||
safe: true
|
safe: true
|
||||||
server: true
|
|
||||||
server_port: 4000
|
|
||||||
baseurl: /
|
baseurl: /
|
||||||
url: http://localhost:4000
|
url: http://localhost:4000
|
||||||
|
|
||||||
|
@@ -5,7 +5,7 @@ isChild: true
|
|||||||
|
|
||||||
## Use the Current Stable Version (5.5) {#use_the_current_stable_version_55_title}
|
## Use the Current Stable Version (5.5) {#use_the_current_stable_version_55_title}
|
||||||
|
|
||||||
If you are just getting started with PHP make sure to start with the current stable release of [PHP 5.5][php-release]. PHP has made great strides adding powerful [new features](#language_highlights) over the last few years. Don't let the minor version number difference between 5.2 and 5.5 fool you, it represents _major_ improvements. If you are looking for a function or it's usage, the documentation on the [php.net][php-docs] website will have the answer.
|
If you are just getting started with PHP make sure to start with the current stable release of [PHP 5.5][php-release]. PHP has made great strides adding powerful [new features](#language_highlights) over the last few years. Don't let the minor version number difference between 5.2 and 5.5 fool you, it represents _major_ improvements. If you are looking for a function or its usage, the documentation on the [php.net][php-docs] website will have the answer.
|
||||||
|
|
||||||
[php-release]: http://www.php.net/downloads.php
|
[php-release]: http://www.php.net/downloads.php
|
||||||
[php-docs]: http://www.php.net/manual/en/
|
[php-docs]: http://www.php.net/manual/en/
|
||||||
|
@@ -24,7 +24,7 @@ You can use [PHP_CodeSniffer][phpcs] to check code against any one of these reco
|
|||||||
like [Sublime Text 2][st-cs] to be given real time feedback.
|
like [Sublime Text 2][st-cs] to be given real time feedback.
|
||||||
|
|
||||||
Use Fabien Potencier's [PHP Coding Standards Fixer][phpcsfixer] to automatically modify your code syntax so that it
|
Use Fabien Potencier's [PHP Coding Standards Fixer][phpcsfixer] to automatically modify your code syntax so that it
|
||||||
conforms with these standards, saving you from fixing each problem by hand.
|
conforms to these standards, saving you from fixing each problem by hand.
|
||||||
|
|
||||||
English is preferred for all symbol names and code infrastructure. Comments may be written in any language easily readable
|
English is preferred for all symbol names and code infrastructure. Comments may be written in any language easily readable
|
||||||
by all current and future parties who may be working on the codebase.
|
by all current and future parties who may be working on the codebase.
|
||||||
|
@@ -18,7 +18,7 @@ interfaces, inheritance, constructors, cloning, exceptions, and more.
|
|||||||
|
|
||||||
### Functional Programming
|
### Functional Programming
|
||||||
|
|
||||||
PHP supports first-class function, meaning that a function can be assigned to a variable. Both user defined and built-in
|
PHP supports first-class function, meaning that a function can be assigned to a variable. Both user-defined and built-in
|
||||||
functions can be referenced by a variable and invoked dynamically. Functions can be passed as arguments to other
|
functions can be referenced by a variable and invoked dynamically. Functions can be passed as arguments to other
|
||||||
functions (feature called Higher-order functions) and function can return other functions.
|
functions (feature called Higher-order functions) and function can return other functions.
|
||||||
|
|
||||||
@@ -39,7 +39,7 @@ can be used interchangeably with anonymous functions in almost all cases.
|
|||||||
|
|
||||||
### Meta Programming
|
### Meta Programming
|
||||||
|
|
||||||
PHP supports various forms of meta programming through mechanisms like the Reflection API and Magic Methods. There are
|
PHP supports various forms of meta-programming through mechanisms like the Reflection API and Magic Methods. There are
|
||||||
many Magic Methods available like `__get()`, `__set()`, `__clone()`, `__toString()`, `__invoke()`, etc. that allow
|
many Magic Methods available like `__get()`, `__set()`, `__clone()`, `__toString()`, `__invoke()`, etc. that allow
|
||||||
developers to hook into class behavior. Ruby developers often say that PHP is lacking `method_missing`, but it is
|
developers to hook into class behavior. Ruby developers often say that PHP is lacking `method_missing`, but it is
|
||||||
available as `__call()` and `__callStatic()`.
|
available as `__call()` and `__callStatic()`.
|
||||||
|
@@ -4,7 +4,7 @@ isChild: true
|
|||||||
|
|
||||||
## Command Line Interface {#command_line_interface_title}
|
## Command Line Interface {#command_line_interface_title}
|
||||||
|
|
||||||
PHP was created primarily to write web applications, but it's also useful for scripting command line interface (CLI) programs. Command line PHP programs can help you automate common tasks like testing, deployment, and application administrativia.
|
PHP was created primarily to write web applications, but it's also useful for scripting command line interface (CLI) programs. Command line PHP programs can help you automate common tasks like testing, deployment, and application administrivia.
|
||||||
|
|
||||||
CLI PHP programs are powerful because you can use your app's code directly without having to create and secure a web GUI for it. Just be sure not to put your CLI PHP scripts in your public web root!
|
CLI PHP programs are powerful because you can use your app's code directly without having to create and secure a web GUI for it. Just be sure not to put your CLI PHP scripts in your public web root!
|
||||||
|
|
||||||
@@ -32,7 +32,7 @@ echo "Hello, $name\n";
|
|||||||
|
|
||||||
PHP sets up two special variables based on the arguments your script is run with. [`$argc`][argc] is an integer variable containing the argument *count* and [`$argv`][argv] is an array variable containing each argument's *value*. The first argument is always the name of your PHP script file, in this case `hello.php`.
|
PHP sets up two special variables based on the arguments your script is run with. [`$argc`][argc] is an integer variable containing the argument *count* and [`$argv`][argv] is an array variable containing each argument's *value*. The first argument is always the name of your PHP script file, in this case `hello.php`.
|
||||||
|
|
||||||
The `exit()` expression is used with a non zero number to let the shell know that the command failed. Commonly used exit codes can be found [here][exit-codes]
|
The `exit()` expression is used with a non-zero number to let the shell know that the command failed. Commonly used exit codes can be found [here][exit-codes]
|
||||||
|
|
||||||
To run our script, above, from the command line:
|
To run our script, above, from the command line:
|
||||||
|
|
||||||
|
@@ -5,7 +5,7 @@ isChild: true
|
|||||||
## PEAR {#pear_title}
|
## PEAR {#pear_title}
|
||||||
|
|
||||||
Another veteran package manager that many PHP developers enjoy is [PEAR][1]. It behaves much the same way as Composer,
|
Another veteran package manager that many PHP developers enjoy is [PEAR][1]. It behaves much the same way as Composer,
|
||||||
but has some noteable differences.
|
but has some notable differences.
|
||||||
|
|
||||||
PEAR requires each package to have a specific structure, which means that the author of the package must prepare it
|
PEAR requires each package to have a specific structure, which means that the author of the package must prepare it
|
||||||
for usage with PEAR. Using a project which was not prepared to work with PEAR is not possible.
|
for usage with PEAR. Using a project which was not prepared to work with PEAR is not possible.
|
||||||
@@ -19,8 +19,8 @@ if version conflicts between two projects arise.
|
|||||||
You can install PEAR by downloading the phar installer and executing it. The PEAR documentation has detailed
|
You can install PEAR by downloading the phar installer and executing it. The PEAR documentation has detailed
|
||||||
[install instructions][2] for every operating system.
|
[install instructions][2] for every operating system.
|
||||||
|
|
||||||
If you are using Linux, you can also have a look at your distribution package manager. Debian and Ubuntu for example
|
If you are using Linux, you can also have a look at your distribution package manager. Debian and Ubuntu, for example,
|
||||||
have a apt ``php-pear`` package.
|
have an apt ``php-pear`` package.
|
||||||
|
|
||||||
### How to install a package
|
### How to install a package
|
||||||
|
|
||||||
|
@@ -57,7 +57,7 @@ database preventing potential SQL injection attacks.
|
|||||||
You should also be aware that database connections use up resources and it was not unheard-of to have resources
|
You should also be aware that database connections use up resources and it was not unheard-of to have resources
|
||||||
exhausted if connections were not implicitly closed, however this was more common in other languages. Using PDO you
|
exhausted if connections were not implicitly closed, however this was more common in other languages. Using PDO you
|
||||||
can implicitly close the connection by destroying the object by ensuring all remaining references to it are deleted,
|
can implicitly close the connection by destroying the object by ensuring all remaining references to it are deleted,
|
||||||
ie. set to NULL. If you don't do this explicitly, PHP will automatically close the connection when your script ends
|
i.e. set to NULL. If you don't do this explicitly, PHP will automatically close the connection when your script ends -
|
||||||
unless of course you are using persistent connections.
|
unless of course you are using persistent connections.
|
||||||
|
|
||||||
* [Learn about PDO connections][5]
|
* [Learn about PDO connections][5]
|
||||||
|
6
_posts/07-01-01-Errors-and-Exceptions.md
Normal file
6
_posts/07-01-01-Errors-and-Exceptions.md
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
---
|
||||||
|
title: Errors and Exceptions
|
||||||
|
---
|
||||||
|
|
||||||
|
# Errors and Exceptions {#errors_and_exceptions_title}
|
||||||
|
|
11
_posts/07-02-01-Errors.md
Normal file
11
_posts/07-02-01-Errors.md
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
---
|
||||||
|
isChild: true
|
||||||
|
---
|
||||||
|
|
||||||
|
## Errors {#errors_title}
|
||||||
|
|
||||||
|
PHP has several levels of error severity. The three most common types of messages are errors, notices and warnings. These have different levels of severity; `E_ERROR`, `E_NOTICE`, and `E_WARNING`. Errors are fatal run-time errors and are usually caused by faults in your code and need to be fixed as they'll cause PHP to stop executing. Warnings are non-fatal errors, execution of the script will not be halted. Notices are advisory messages caused by code that may or may not cause problems during the execution of the script, execution is not halted.
|
||||||
|
|
||||||
|
Another type of error message reported at compile time is the `E_STRICT` message, these messages are used to suggest changes to your code to help ensure best interoperability and forward compatibility for your code.
|
||||||
|
|
||||||
|
* [Predefined Constants for Error Handling](http://www.php.net/manual/en/errorfunc.constants.php)
|
@@ -17,7 +17,7 @@ obvious.
|
|||||||
|
|
||||||
Another problem is when classes automatically throw an error to the screen and exit the process. When you do this you
|
Another problem is when classes automatically throw an error to the screen and exit the process. When you do this you
|
||||||
stop another developer from being able to dynamically handle that error. Exceptions should be thrown to make a developer aware
|
stop another developer from being able to dynamically handle that error. Exceptions should be thrown to make a developer aware
|
||||||
of an error, then they can choose how to handle this. E.g:
|
of an error; they then can choose how to handle this. E.g.:
|
||||||
|
|
||||||
{% highlight php %}
|
{% highlight php %}
|
||||||
<?php
|
<?php
|
@@ -6,13 +6,13 @@ isChild: true
|
|||||||
|
|
||||||
Eventually everyone builds a PHP application that relies on user login. Usernames and passwords are stored in a database and later used to authenticate users upon login.
|
Eventually everyone builds a PHP application that relies on user login. Usernames and passwords are stored in a database and later used to authenticate users upon login.
|
||||||
|
|
||||||
It is important that you properly [_hash_][3] passwords before storing them. Password hashing is an irreversible, one way function performed against the users password. This produces a fixed-length string that can not be feasibly reversed. This means you can compare a hash against another to determine if they both came from the same source string, but you can not determine the original string. If passwords are not hashed and your database is accessed by an unauthorized third-party, all user accounts are now compromised. Some users may (unfortunately) use the same password for other services. Therefore, it is important to take security seriously.
|
It is important that you properly [_hash_][3] passwords before storing them. Password hashing is an irreversible, one way function performed against the user's password. This produces a fixed-length string that cannot be feasibly reversed. This means you can compare a hash against another to determine if they both came from the same source string, but you cannot determine the original string. If passwords are not hashed and your database is accessed by an unauthorized third-party, all user accounts are now compromised. Some users may (unfortunately) use the same password for other services. Therefore, it is important to take security seriously.
|
||||||
|
|
||||||
**Hashing passwords with `password_hash`**
|
**Hashing passwords with `password_hash`**
|
||||||
|
|
||||||
In PHP 5.5 `password_hash` was introduced. At this time it is using BCrypt, the strongest algorithm currently supported by PHP. It will be updated in the future to support more algorithms as needed though. The `password_compat` library was created to provide forward compatibility for PHP >= 5.3.7.
|
In PHP 5.5 `password_hash` was introduced. At this time it is using BCrypt, the strongest algorithm currently supported by PHP. It will be updated in the future to support more algorithms as needed though. The `password_compat` library was created to provide forward compatibility for PHP >= 5.3.7.
|
||||||
|
|
||||||
Below we hash a string, we then check the hash against a new string. Because our two source strings are different ('secret-password' vs. 'bad-password') this login will fail.
|
Below we hash a string, and then check the hash against a new string. Because our two source strings are different ('secret-password' vs. 'bad-password') this login will fail.
|
||||||
|
|
||||||
{% highlight php %}
|
{% highlight php %}
|
||||||
<?php
|
<?php
|
@@ -10,6 +10,6 @@ be followed:
|
|||||||
- It is recommended that you store your configuration information where it cannot be accessed directly and pulled in
|
- It is recommended that you store your configuration information where it cannot be accessed directly and pulled in
|
||||||
via the file system.
|
via the file system.
|
||||||
- If you must store your configuration files in the document root, name the files with a `.php` extension. This
|
- If you must store your configuration files in the document root, name the files with a `.php` extension. This
|
||||||
ensures that, even if the script is accessed directly, it will not be outputed as plain text.
|
ensures that, even if the script is accessed directly, it will not be output as plain text.
|
||||||
- Information in configuration files should be protected accordingly, either through encryption or group/user file
|
- Information in configuration files should be protected accordingly, either through encryption or group/user file
|
||||||
system permissions
|
system permissions
|
@@ -1,4 +1,4 @@
|
|||||||
# Caching {#caching_title}
|
# Caching {#caching_title}
|
||||||
|
|
||||||
PHP is pretty quick by itself, but bottlenecks can arise when you make remote connections, load files, etc.
|
PHP is pretty quick by itself, but bottlenecks can arise when you make remote connections, load files, etc.
|
||||||
Thankfully, there are various tools available to speed up certain parts of your application, or reduce the number of times these various time consuming tasks need to run.
|
Thankfully, there are various tools available to speed up certain parts of your application, or reduce the number of times these various time-consuming tasks need to run.
|
@@ -149,6 +149,96 @@ application, as the object using the shared or global resource requires no knowl
|
|||||||
|
|
||||||
* [Singleton pattern on Wikipedia](https://en.wikipedia.org/wiki/Singleton_pattern)
|
* [Singleton pattern on Wikipedia](https://en.wikipedia.org/wiki/Singleton_pattern)
|
||||||
|
|
||||||
|
## Strategy
|
||||||
|
|
||||||
|
With the strategy pattern you encapsulate specific families of algorithms allowing the client class responsible for
|
||||||
|
instantiating a particular algorithm to have no knowledge of the actual implementation.
|
||||||
|
There are several variations on the strategy pattern, the simplest of which is outlined below:
|
||||||
|
|
||||||
|
This first code snippet outlines a family of algorithms; you may want a serialized array, some JSON or maybe
|
||||||
|
just an array of data:
|
||||||
|
{% highlight php %}
|
||||||
|
<?php
|
||||||
|
|
||||||
|
interface OutputInterface
|
||||||
|
{
|
||||||
|
public function load();
|
||||||
|
}
|
||||||
|
|
||||||
|
class SerializedArrayOutput implements OutputInterface
|
||||||
|
{
|
||||||
|
public function load()
|
||||||
|
{
|
||||||
|
return serialize($arrayOfData);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class JsonStringOutput implements OutputInterface
|
||||||
|
{
|
||||||
|
public function load()
|
||||||
|
{
|
||||||
|
return json_encode($arrayOfData);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class ArrayOutput implements OutputInterface
|
||||||
|
{
|
||||||
|
public function load()
|
||||||
|
{
|
||||||
|
return $arrayOfData;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
{% endhighlight %}
|
||||||
|
|
||||||
|
By encapsulating the above algorithms you are making it nice and clear in your code that other developers can easily
|
||||||
|
add new output types without affecting the client code.
|
||||||
|
|
||||||
|
You will see how each concrete 'output' class implements an OutputInterface - this serves two purposes, primarily it
|
||||||
|
provides a simple contract which must be obeyed by any new concrete implementations. Secondly by implementing a common
|
||||||
|
interface you will see in the next section that you can now utilise [Type Hinting](http://php.net/manual/en/language.oop5.typehinting.php) to ensure that the client which is utilising these behaviours is of the correct type in
|
||||||
|
this case 'OutputInterface'.
|
||||||
|
|
||||||
|
The next snippet of code outlines how a calling client class might use one of these algorithms and even better set the
|
||||||
|
behaviour required at runtime:
|
||||||
|
{% highlight php %}
|
||||||
|
<?php
|
||||||
|
|
||||||
|
class SomeClient
|
||||||
|
{
|
||||||
|
private $output;
|
||||||
|
|
||||||
|
public function setOutput(OutputInterface $outputType)
|
||||||
|
{
|
||||||
|
$this->output = $outputType;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function loadOutput()
|
||||||
|
{
|
||||||
|
return $this->output->load();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
{% endhighlight %}
|
||||||
|
|
||||||
|
The calling client class above has a private property which must be set at runtime and be of type 'OutputInterface'
|
||||||
|
once this property is set a call to loadOutput() will call the load() method in the concrete class of the output type
|
||||||
|
that has been set.
|
||||||
|
{% highlight php %}
|
||||||
|
<?php
|
||||||
|
|
||||||
|
$client = new SomeClient();
|
||||||
|
|
||||||
|
// Want an array?
|
||||||
|
$client->setOutput(new ArrayOutput());
|
||||||
|
$data = $client->loadOutput();
|
||||||
|
|
||||||
|
// Want some JSON?
|
||||||
|
$client->setOutput(new JsonStringOutput());
|
||||||
|
$data = $client->loadOutput();
|
||||||
|
|
||||||
|
{% endhighlight %}
|
||||||
|
|
||||||
|
* [Strategy pattern on Wikipedia](http://en.wikipedia.org/wiki/Strategy_pattern)
|
||||||
|
|
||||||
## Front Controller
|
## Front Controller
|
||||||
|
|
||||||
The front controller pattern is where you have a single entrance point for you web application (e.g. index.php) that
|
The front controller pattern is where you have a single entrance point for you web application (e.g. index.php) that
|
||||||
|
Reference in New Issue
Block a user