php[architect] November 2018

(singke) #1
20 \ November 2018 \ http://www.phparch.com

Getting Started With Php? Let’s Start the Right Way!

Combined with namespaces it changed
the entire PHP community, making
releasing and using packages easy. Now
you have some specific packages for
solving specific problems, like a pattern.

Traits


Traits make PHP more fun. They have
a similar syntax with classes, but it’s not
a class, it’s a Trait. Think of Traits like a
way to share behavior (using methods
and properties) across multiple classes.
Let’s say you have two classes: Bird
and Chicken. Birds can fly, but they
can also walk. Chickens can only walk.
Thinking about the code, we can create
two Traits: Walkable and Flyable. You
are able to allow a bird to fly and walk,
but you can allow a chicken to walk, not
fly as in Listing 1.
So, think of Traits like behaviors
decoupled from inheritance. It’s also
an excellent approach to name them
like that. You’ll be able to share those
methods or properties among any
other classes, reusing code and making
it readable (a good name for a Trait,
BTW).

Shortened Array Syntax
A simple, but a good tip: starting
from here, it’s a good approach to use
[] to declare arrays. Instead of using
array(1, 2, 3) just use [1, 2, 3]; it’s
easier and clearer.

Built-In Web Server
When I started programming in PHP,
I had to install Apache locally to have
a working PHP environment. PHP
needs a web server to run, and for years
the Apache was the only, or most well-
known, option.
Since PHP 5.4, the only thing you
need to get started is to have PHP
installed. Install it and start a develop-
ment web server in the specific port you
want:

php -S localhost:8000

You can visit your favorite browser
on http://localhost:8000, and you’re
ready to go. You have a web server
opened in the current directory you’re

in. If you need to specify another direc-
tory, just use the -t option: -t ./public,
for example.

Composer: the Package
Manager for PHP
Composer is a fantastic project
started by Nils Adermann and Jordi
Boggiano in 2011, with the first release
in 2012. It was really a game changer,
allowing you to create PHP packages
and then require them in your app and
use them via namespaces and its auto-
load support. Composer is written in
PHP and distributed in a .phar file so
it’s very easy to use. It’s a standard for
PHP nowadays.
Composer organized things in PHP.
Before, you needed a bunch of require
and include calls in your code, copying
and pasting files between projects or
keeping all third-party code in your
own repositories. After Composer
packages became more professional
and versioned, it became a standard.
Just require what version of which
package you need to use via composer.
json, ask Composer to install it, include
the autoloader, and use it in your class;
it’s that simple.
Composer made PHP more profes-
sional by respecting package versions,
avoiding conflicts, and making updates
and upgrades easier. It also allowed
us to share features and components
between frameworks and applications,
allowing you to use a component from
framework X and another component
from framework Y. This possibility
allowed us to start thinking in coding
conventions—another problem PHP
had some time ago.
That’s why the PHP-FIG^4 was created,
for developing recommendations (not
rules) for writing good PHP code. The
group started in 2009 but things started
changing in 2011 and 2012, when the
first PSR (PHP Standard Recommenda-
tion) was released.
After that, together with Packagist^5
(a public repository for Composer
packages) many newly developed

4 PHP-FIG: https://www.php-fig.org
5 Packagist: https://packagist.org

PHP components/libraries started
following the same code conventions
(by PHP-FIG), using namespaces to
organize the code, and sharing them
through Packagist and Composer.
Do you want to add a package to
work easily with images in your PHP
project, for example?

composer require intervention/image

And be happy!

PHP 5.5 And 5.6—Before
the Big Move
In 2013 and 2014 we had the launch
of PHP 5.5 and 5.6, respectively. A lot of
new features were introduced, like:


  • Added scalar class name resolution
    via ::class;

  • Added the finally keyword for
    exceptions;


Listing 1


  1. trait Flyable

  2. {

  3. public function fly()

  4. {

  5. // Go, fly!

  6. }

  7. }



  8. trait Walkable

  9. {

  10. public function walk()

  11. {

  12. // Go, walk!

  13. }

  14. }



  15. class Bird

  16. {

  17. use Flyable, Walkable;

  18. }



  19. class Chicken

  20. {

  21. use Walkable;

  22. }



  23. $bird = new Bird();

  24. $chicken = new Chicken();



  25. $bird->walk();

  26. $bird->fly();

  27. $chicken->walk();

Free download pdf