php[architect] November 2018

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

FEATURE

spl_


Joshua ThijssenHis passion lies in high-end and complex internet systems, code optimization and server administration. His programming is a freelance consultant, trainer and developer.
skills include-but are not limited to-PHP, C, Java, and Python, and he has experience on a wide range of operating systems. He is a regular speaker at international conferences and speaks
about a wide variety of subjects. You can find his blog on http://www.adayinthelifeof.nl.


Mastering
the SPL Library
by Joshua Thijssen

The Standard PHP Library (SPL) has recently gained popularity among PHP developers. With more complex applications and more data to process, the library’s vast
functionality can make development easier and more efficient, but the documentation for the SPL falls far behind PHP’s core documentation.
Mastering the SPL Library - the facets of the library, including background information where needed. Each entry is illustrated with code examples a php[architect] guide covers all
to give you an idea of how to use it. After reading this book, you will be ready to use the SPL interfaces, data structures, and - of course - the iterators.
This book is perfect for those ready to begin using the SPL library and for those already familiar with it who wish to learn the ins and outs of its more advanced features. With its
detailed information and code examples, this book is a great reference for all SPL users. Developers will want it on their desks at all times.


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


Junior Grossi


Maybe you’re here because you’re just beginning with PHP, or maybe you worked with PHP
some time ago and would like to catch up. Perhaps you’re used to your way of working with
PHP and haven’t adopted the newest additions to the language. There are many changes and
the language is still improving. Let’s go back a bit in its history.

The PHP language is the most used
programming language for web devel-
opment purposes. It’s present in around
80% of the web, and that’s good because
you have more job offers, more devel-
opers working with it, but there are also
more problems to fix.
It’s really easy to get started with PHP,
and because of that, it’s also easy to start
it the wrong way. The PHP language
we’re talking about is not the same as
it was ten years ago. The language has
improved and it’s getting better with
time. You must understand what’s
currently happening to understand
the best place to begin. It’s not as easy
as just finding a blog post from 2008
and then writing some code. That won’t
work; believe me.
There are many companies hiring
PHP developers. They’re also look-
ing for good code writers, developers
that know what they’re doing and why,
using tested and resilient object-ori-
ented approaches we have nowadays in
the market.
Let’s go through some changes that
came to the PHP world since 2009
which make PHP a better and more
professional language to work with. As
you know, PHP 5 started with good
object-oriented programming support
especially compared to PHP 4, but
things started changing in 2009 when
PHP 5.3 was released.

The Waterfall: PHP 5.3
PHP 5.3 started with two new features
that changed the game forever: name-
spaces and lambda functions/closures.

Namespaces
PHP always was a dangerous place
to go when talking about packages and

libraries. You could have two classes
with the same name, but belonging to
different packages or libraries. That
made developers use naming conven-
tions like Package_Foo as a class name.
In this case, Package would be a sample
package name, just to allow you to have
another OtherPackage_Foo class.
With PHP 5.3 this problem was
solved by adding support for name-
spaces^1. This allowed us to group classes
in a package context, making it easy to
write code in a simpler and more orga-
nized way. The same classes above were
now named Package\Foo and OtherPack-
age\Foo, respectively.

namespace Package;

class Foo
{
// some code
}

It’s now easy to call those classes. You
can call them with the full namespace,
like new \Package\Foo() or with use and
the full class namespace in the top of
the current PHP file, referencing it as
just Foo in your code:

use Package\Foo;

$instance = new Foo();

Lambda Functions/Closures
That was not a feature that radically
changed how PHP works. It did, however,
make PHP easier to understand when
talking about behaviors, allowing you
to group a set of commands in a “call-
able” anonymous function, allowing
you to call it whenever you need to.

1 support for namespaces:
https://wiki.php.net/rfc/namespacecurlies

Before PHP 5.3 all functions had
to be named name. After PHP 5.3 we
started having anonymous functions, or
lambda functions, or even just closures^2.
Now you could attach a function to a
variable, and you could also call that
function/variable whenever you want
or pass it as a parameter to another
function.

$function = function () {
echo 'Hello World!';
}
$function(); // "Hello World!"

This new feature allowed some new
frameworks to start using it for routes,
for example, making the code much
simpler and clearer:

$app->route('/foo', function () {
// route logic here
});

Before PHP 5.3 that was only possi-
ble by using create_function but the
code wouldn’t be as clear as what you
saw above. create_function was essen-
tially a wrapper for eval, so it also had
security implications.

March 2012: Professional
Improvements
When PHP 5.4 was released in
2012, it introduced interesting features
like Traits, shortened array syntax, a
built-in web server, and also improved
performance and reduced memory
consumption.
In addition to the release of PHP 5.4,
2012 was a very important year; the year
Composer^3 was first released. Composer
is the package manager for PHP, period.

2 closures: https://wiki.php.net/rfc/closures
3 Composer: http://getcomposer.org
Free download pdf