Pro PHP- Patterns, Frameworks, Testing and More

(vip2019) #1
CHAPTER 5 ■ WHAT'S NEW IN PHP 6^47

Array
(
[0] => limonada
[1] => llaves
[2] => luna
)
Array
(
[0] => limonada
[1] => luna
[2] => llaves
)

Precisely which methods will be upgraded to support Unicode semantics is not yet clear.
fopen() will likely be updated to extend the rt reading mode to read Unicode documents.
Additionally, most of the string methods—strlen(), str_word_count(), and so on—will be
updated, and you will be able to use various SPL iterators to iterate over strings by character or
by word.
You can find more information about PHP 6 Unicode support at http://www.php.net/Unicode.

Namespaces


Namespaces are to PHP classes as directories are to files—they add structure and hierarchical
organization to class libraries. Namespaces allow you to use the same class name for two distinct
classes. For example, you might wish to have a class named Line, which draws a line on an
image. However, the way your Line class would function would be very different for raster or
vector images. The ideal solution is to use two classes, and with namespaces, you can give
them both the same class name.
Namespaces make use of two key language constructs: namespace and use. To declare a
namespace, specify the namespace name at the beginning of a file. All the classes and functions
declared in this file will now belong to the namespace. The namespace line must be at the top
of the file, before any other variable, class, or function declarations. Listing 5-8 demonstrates
how to create a class within a namespace.

Listing 5-8. Declaring a Namespace (Vector.php)

<?php
namespace Vector;

class Line {
public function draw($x1, $y1, $x2, $y2) {... }
}

The scope resolution operator (::) has been overloaded for use with namespaces. For
example, if you wanted to create a new instance of your Line class in the Vector namespace,
you would use the scope resolution operator, as shown in Listing 5-9.

McArthur_819-9C05.fm Page 47 Wednesday, February 27, 2008 8:38 AM

Free download pdf