2004.07_Php 5-Discover Why the Php 5 Language Has Such a Loyal Following.pdf
(
2596 KB
)
Pobierz
Layout 1
PROGRAMMING
PHP 5
Onward and upward
Of all the programming languages to
have emerged in recent years, PHP is
one of the great success stories. It has
fostered a loyal community of devel-
opers and, despite some well
documented issues, still maintains its
popularity.
BY STEVEN GOODWIN
D
oes its latest incarnation, PHP 5,
find the original documentation lurking
at [1].
PHP as we know it really began with
version 3, released in June 1998. Unlike
the upgrade status to PHP/FI 2.0, PHP 3
was a complete rewrite by Rasmus,
along with Andi Gutmans and Zeev
Suraski. From a technical standpoint, it
was very much improved, and more
capable of dealing with high volume web
sites and e-commerce applications. It
also featured the first signs of object ori-
entation (see Box: Uh Oh!), and a
change of acronym to the still-in-use,
PHP: Hypertext Preprocessor.
Ve rsion 4 was essentially an extension
of 3, using the same syntax and seman-
tics. It featured output buffering,
improved object orientation, and various
new features (like references) and
instructions, such as the
foreach
control
structure. Behind the scenes too, a lot
had changed, as the PHP code base had
been modularized and optimized, with
some code running over 200 times faster.
This was largely thanks to a new
engine, known as
Zend
, which was a
complete re-write of the original. The
PHP community was also growing by
leaps and bounds: a number of third
party databases were now supported, the
PEAR repository came into being, and
the documentation had grown into a
comprehensive, and very usable, online
manual [2], currently available in 26 dif-
ferent languages.
PHP5 is claimed to be the latest and
best version of PHP, utilizing another
new code engine (Zend 2), an impressive
new object model and a host of new fea-
You’re History
The first version of PHP was written in
1994 by Rasmus Lerdorf and constituted
a number of Perl scripts for building web
pages. The term PHP/FI was coined in
1995, and was followed by PHP/FI 2.0 in
1997, with PHP/FI standing for
Personal
Home Page / Forms Interpreter
. Even
though some of the functionality that we
know today was present in these early
versions, beyond a small cult group of
developers, it was largely unheard of.
Curious readers and history students can
Uh Oh!
Object Orientation is more a paradigm than
it is any particular type of programming.
When used correctly, its benefits include
code re-use, abstraction of detail, data
encapsulation and polymorphism. The
implementation of an OO solution involves
objects
,a much over-used term that (in this
instance) indicates that both the data, and
the code that manipulates it, are bound
together in one unit. Each
object
is often
represented by a class. The data held within
this class represents its current state, and
the code that affects it are called
methods
,
or
member functions
.
When builders go down
the pub they talk about
football. Presumably
therefore, when foot-
ballers go down the
pub they talk about
builders! When Steven Goodwin goes
down the pub he doesn’t talk about
football. Or builders. He talks about
computers. Constantly…
64
July 2004
www.linux-magazine.com
The Next Generation
do justice to its heritage, or is it
destined to become a skeleton in
the closet? Steven Goodwin finds out.
PHP 5
PROGRAMMING
tures. It has gone through four beta ver-
sions with, at the time of writing, the
current version standing at Release Can-
didate 2, made available on the 25th
April 2004. It is claimed stable, but
“still
not recommended for mission-critical
use”
. True stability will come with time,
although the experiments of this author
have shown it perfectly usable for small
to medium scale work.
vice to the backwards compatibility
issue. The core language looks no differ-
ent to that of PHP 4 and although there
have been a few backwards
in
compati-
ble changes, these have been in the
name of stricter standards, and are
detailed in [5].
If your code is polite and follows the
described function behavior to the letter,
most of these changes won’t affect you,
but be warned as
array_merge
now only
accepts arrays. This may cause
E_WARN-
ING
s to appear without apparent reason.
It is worthwhile to note that illegal string
offsets are now considered errors (not
warnings). Note also that,
capitalized) are used for the extensions
like SQLite and SOAP.
There are a slew of new functions in
PHP 5, all detailed in [6]. Most of them
provide better support for the core lan-
guage, although most have been
available before in libraries, or as user-
added comments in the standard
documentation on the main PHP site [2].
These functions break down into five
basic groups. See Table 1.
Before we launch into specific details
of some of these features, there are two
development functions to highlight. The
first is
var_dump
which has an improved
look, and expands the information inside
the class, as does
print_r
.
Little Fluffy Clouds
The source code for PHP 5 can be down-
loaded from [3] as a five MByte tarred
gzip file, although the only official bina-
ries available are for Windows. Brave
Debian users can grab the
.deb
s from [4]
by adding the line,
$app.is_object()
deb http://packages
U
.dotdeb.org ./
returns an object and one needs to use
the modern form,
// Example of var_dump
object(MyName)#5 (2) {
["firstName"]=>
string(6) "Steven"
["lastName"]=>
string(7) "Goodwin"
to their
/etc/apt/sources.list
, although
this will not be guaranteed to work.
Assuming you’re compiling from source
the traditional trio is employed thus,
is_object($app)
to get a boolean value. However, most
practical code will not (or should not!)
have these errors present. For the termi-
nally paranoid there is a compatibility
mode. This can be used to determine
whether the bug is in your code, or the
new PHP 5 interpreter. It can be con-
trolled with the line,
}
$ ./configure --with-mysql
U
--with-apxs
$ make
# make install
The second is a new function called
debug_backtrace
, which returns an array
showing the call stack. This has been
partially back ported into PHP 4, but
looks to be a stalwart for all PHP devel-
opers looking for quick, light weight,
debugging assistant, without resorting to
xdebug
.
The options for
./configure
may need to
be adapted, depending on whether you
have (or want) MySQL support. The pri-
mary dependency essential for PHP 5 is a
recent version of
libxml2
(version 2.5.10
or above), but once that’s available, a
basic compile and install can be accom-
plished with a fairly minimal time outlay.
Documentation is included in the pack-
age, which also details how to run PHP 4
and 5 alongside each other. If you’re
compiling PHP 5 as an Apache module,
then the traditional rules for building
DSO’s (Dynamic Shared
Object) apply.
zend.ze1_compatibility_mode
U
=Off
The Invisible Man
Among the list of core language features
there’s a small selection that has been
devoted to code that doesn’t exist! That
is, if a non-existent class method is called,
the special
__call
function is invoked.
in your
php.ini
file. Two sample
ini
files
come with PHP 5, a development version
and a more secure release version.
Although neither sees the need to use
the compatibility mode.
Developers working on the bleeding
edge will also have realized that studly-
Caps (first word in lower case, and the
initial letter of every other word being
class Reverse {
function __call($function,
U
$arguments)
{
Table 1: New function groups
return strrev($function);
}
A Grand Don’t Come
For Free
The first, most obvious, ques-
tion with such a new system
is a consideration of old code.
Does it break anything? Is
this a backwards step? Fortu-
nately, the answer appears to
be no. Most of the changes in
this release have paid lip ser-
Area
Number Notes
}
Arrays
6
Mostly to determine differences between
arrays
InterBase
19
Various functions for Interbase users
$r = new Reverse;
print $r->this_doesnt_exist();
iconv
7
Handles internationalization features, and
MIME headers
Streams
8
Most supplying useful low level socket
handling functionality
Or, if you try to access a member vari-
able that doesn’t exist, one of two
different functions will be called,
depending on whether you tried to read
from, or write to, the variable.
Misc
34
General purpose functions, including PHP
syntax checker, string conversion and process
control
www.linux-magazine.com
July 2004
65
PROGRAMMING
PHP 5
01 class Length
02 {
03 function __get($variable_
U
name)
04 {
05 return strlen($variable_
U
name);
the appropriate class files at the start,
compared to sporadic on-demand load-
ing (which may include disk seek times)
throughout the script’s lifetime.
Be careful when using
__autoload
since file names are case-sensitive, but
PHP class names are not. This could
cause missing classes to try and load
non-existent files from disk. Porting code
from a Windows environment will often
include at least one of this variety!
ples, which makes it easier for PHP 5
developers to interact with them.
There are two main areas of object ori-
ented study in PHP 5. The first are the
changed features. These are cases where
improvements have been made to the
language that allow traditional OO
design patterns to be used natively
within PHP. These were possible previ-
ously, albeit with some minor code
hacks. The second involves new features
that haven’t been technically possible
before, because of the language design.
Let us cover the improvements first.
The biggest improvement is the way
references and object copying occur.
Previously, the object model was to
always copy objects as they were passed
into, and out of, func-
tions. This made it
difficult to handle
objects efficiently, and
so references were
added. Any PHP code
with extensive object
use would therefore be
full of the
&
symbol, and omissions
could lead to hard-to-spot bugs.
06 }
07
08 function __set($variable_
U
name, $value)
09 {
10 print "I can't set the
U
length of $variable_name
U
to $value!";
11 }
12 }
13
14 $len = new Length;
15 $len->thisname = 10;
16 print $len->thisname;
The Girl in the Other Room
Of all the new features in PHP 5, one cat-
egory stands proud: object orientation.
OO features have been part of PHP since
version 3, although they have always
been rather minimal in scope. PHP has
Table 2: Member Access
Type
Accessible to own class
…to derived classes
..to the world
Public
Yes
Yes
Yes
All three of these functions only work as
class methods, so you can not have a
global
__set
function to cope with every
non-handled case.
On the surface, these might appear as
quirky functions, but of little use. How-
ever, they do have a number of serious
applications. The
__call
function, for
example, could be used to create mock
objects, while
__set
and
__get
can pass
information to and from a database,
while remaining completely transparent
to the end user.
Another feature of this ilk is
__autoload
, which gets called whenever
a non-existent class is instantiated. The
classic example for this feature is to load
class files on demand. For example,
Protected
Yes
Yes
No
Private
Yes
No
No
never been an OO language, merely a
language that happens to support some
object oriented features – namely classes.
However, PHP’s previous support for
OO had very limited encapsulation and
data hiding capabilities, making it
unsuitable for large projects without
requiring a great deal of care. PHP 5 adds
such a wealth of OO functionality that in
one fell swoop it sits close to OO-Perl
and Ruby. As a paradigm, OO is well
understood and (despite the bad press
attributed it, thanks to the nuances – and
nuisances – of C++) is quite suitable
for large scale operations. Many toolkits,
such as GTK, are based on OO princi-
01 class MyName {
02 var $name;
03 }
04
05 function ChangeName
U
(&$newname)
06 {
07 $newname->name = "Steev";
08 }
09
10 $me = new MyName;
11 ChangeName($me);
12 print $me->name;
function __autoload($classname)
{
require_once "$classname.inc";
}
Listing 1: London pubs
01 // sample file in boxout: London pubs
02 $pub_list = simplexml_load_file('londonpubs.xml');
03
04 // iterate through each 'pub' element
05 foreach ($pub_list as $pub) {
06 if ($pub->name == $rate_this_pub) { // check name element
07 $pub->name['rating'] = $rating_for_pub; // write a rating
attribute
08 print "Rating changed for $pub->name, to $rating_for_pub";
09 }
10 }
11
12 $new_xml_data = $pub_list->asXML();
13 file_put_contents("newlondonpubs.xml", $new_xml_data);
$me = new UnknownName;
// Causes UnknownName.inc to
U
be loaded before class creation
This allows the script to start and run
very quickly, only loading additional
class resources when it needs to, allow-
ing larger class hierarchies to be
employed without degraded the per-
formance in simpler cases. High
performance environments must be
careful to consider the load time for all
66
July 2004
www.linux-magazine.com
PHP 5
PROGRAMMING
This simple example, to print the name
‘Steev’, would fail to work if the
&
was
omitted in line 5, as a brand new copy of
the
MyName
class would have been cre-
ated and assigned to $newname. The
name ‘Steev’ would then have been
assigned to this new copy, and not the
object passed in from line 11.
With PHP 5, class objects are passed
as a reference by default, not value, so
the
&
symbol is no longer needed, mak-
ing it less error prone. The reference
syntax is still maintained for compatibil-
ity.
function) with the word
private
, and this
will be enforced.
Cornflake Girl
Another important change has been with
the constructors. Instead of creating a
function with an identical name to the
class,
01 class MyName {
02 private $name;
03 }
04
05 $me = new MyName;
06 print $me->name;
class MyName {
function Myname()
{
// This is the constructor,
U
called whenever a new
U
MyName is created!
}
Which produces the error,
Fatal error
: Cannot access
U
private property MyName::$name
U
in
somefile.php
on line
6
}
05 function ChangeName($newname)
a constructor can now be created by
using the new name
__construct
. At a
first casual glance, this appears, on the
surface, to only be a minor change, how-
ever it becomes a major time saver if you
need to change the class hierarchy, since
calling the parent class’s constructor
now becomes,
The use of this new syntax is encour-
aged, and should lead to the older
explicit references being deprecated in
future versions.
The only problem with this feature is
that the code appears to be run-time
compatible with versions 3 and 4 – but
isn’t! In the rare cases when your older
code relied of the copying of objects, you
will now need to
clone
them instead.
This is a new feature, made necessary
because of the new object model. Calling
the
__clone
function will make a bitwise
copy of the existing object, as will the
more traditional,
Even derived classes will not be able to
use
$this->name
to access the variable.
Any attempt to do so will cause PHP to
create another public variable that be-
longs to the derived class. This can cause
subtle errors, but private members and
methods are easy enough to learn, and
can add great security to your code, es-
pecially when you’re developing
modules.
Protected
is a further extension of pri-
vate. Any member that is
protected
can
be accessed by the class itself (just like
private
), but it can also be used by any
derived class, such as
MyFullName
:
class MyFullName extends
U
MyName {
function __construct()
{
parent::__construct();
}
}
$new_object = clone $old_object;
class MyFullName extends
U
MyName { ...
In large formal systems, the class hierar-
chy should not be changed often (if at
all) as this can break the design. How-
ever in smaller ad-hoc systems this can
prevent bugs from creeping in when new
classes are added in between a parent
and its child.
There only appears to be one primary
limitation with constructors and even
that limitation is fairly inconsequential
in practice. That is, you can not overload
the constructor as you can in other
Object Orientated languages. This limits
the number of ways you can create the
object:
Furthermore, any class that overloads
the
__clone
function can dictate how it
should be copied, for cases where refer-
ence counting is required, or where the
standard copy is not good enough.
See Table 2, on the previous page, for a
quick breakdown of these security defin-
itions.
In addition, there is a new keyword
called
final
which can be added to a
member function to prevent it from
being derived or extended any further.
All member variables, and methods,
are created as
public
by default. This
ensures backwards compatibility, and
only functions called
public
,
private
or
protected
are likely to have problems.
Guns Of Brixton
Probably the most visible addition to
PHP’s OO handling code is represented
by three new keywords: public, private
and protected. Under PHP 4, every mem-
ber variable of a class could be read by
anyone. From any code. Placed any-
where.
Such variables are termed
public
, in
much the same way as permissions in
the Linux filesystem. By convention, any
variable prefixed with an underscore was
considered
private
and should only be
accessed by the class itself. However,
this is only a convention, and before
PHP 5 could not be guaranteed. Now it is
possible to prefix a member variable (or
London Pubs
A sample XML file used in the example might appear thus:
<pubs>
<pub><name>All Bar One</name><address>26-30 London Bridge
U
Street, London, SE1 2SZ </address></pub>
<pub><name>The Banana Store</name><address>1 Cathedral Street,
U
London, SE1 9DE </address></pub>
<pub><name>The Barrowboy and Banker</name><address>6-8,
U
Borough High Street, London, SE1 9QQ </address></pub>
</pubs>
www.linux-magazine.com
July 2004
67
PROGRAMMING
PHP 5
//Not directly available in PHP5
$me = new MyName("Steev");
$full_me = new MyName("Steven",
U
"Goodwin");
(such as database locks or file handles)
may not. Such functions are declared
using the name
__destruct
.
Backwards compatibility is still an
issue in OO, and fortunately, there are
precious few cases that could fail. How-
ever, any member function called
__construct
or
__destruct
will cause the
code to behave in an undetermined man-
ner (especially if the function name
doesn’t reflect its traditional purpose).
As will any functions using the other
new keywords.
08
09 $this->local =& $single_
U
var;
10 }
11 }
If you need this functionality, I suggest
using default parameters in the construc-
tor and patching the data accordingly.
So, if you wanted to know how many
MyName
classes had been created, in
PHP 5 you could write:
function __construct($name1="",
U
$name2="")
01 class MyName {
02 static $class_count = 0;
03
04 function MyName()
05 {
06 MyName::$class_count++;
07 }
08 }
09
10 $you = new MyName;
11 $me = new MyName;
12 $them = new MyName;
13 $everybody = new MyName;
14
15 print MyName::$class_count;
{
if ($name2 == "") {
$this->name = $name1;
U
// no surname
} else {
$this->name = "$name1 $name2";
Stuck On You
Another very welcome improvement is
the ability to use static methods and
member variables. A static member is
one that is identical across every
instance of the class. Previously, it was
possible to write code that
looked
like
static members were available, but the
popular method of doing so (given
below) requires extra memory.
}
}
It is also possible for constructors to be
made private. This trick prevents other
classes from creating specific objects, as
is required when implementing single-
tons, as we’ll see shortly.
Destructors are a welcome addition to
PHP. They are called automatically
whenever an object is destroyed, which
traditionally occurs when the code has
finished executing. Although the mem-
ory that a class uses is always reclaimed
automatically, any external resources
01 class PHP4FakeStatic
02 {
03 var $local;
04
05 function PHP4FakeStatic()
06 {
07 static $single_var=0;
Static members do not have a specific
instance of the class, so they
must
be
set (and read) without
$this
, as seen
in line 6. Similarly, member functions
can be called directly using the
class
name::function name()
syntax. How-
ever, be warned that because static
members exist outside of any specific
instance, it is not possible to access any
non-static data from within the class.
Any static function that tries to use
$this
will be greeted with the error,
PHP 5 Features in Brief
Feature Notes
Object references Removes need for &
Constructors and destructors Now have unified name, regardless of class name
Public/private/protected For data hiding
Static members One copy of a variable for all instances of call
Final keyword Prevents further derivation of classes
Class constants Use
const cvar = 3.1415
instead of define
Objects copied by __clone function Needed now objects are passed by reference
Type hinting
Indicates the required type for a parameter. A nod to a more strongly-typed PHP
Fatal error
: Using $this when
U
not in object context in
U
somefile.php
on line
6
Instance of
It is possible to determine the type of a dynamic object with
$foo instanceof
SomeClass
This construct can also be used to create
a singleton, as mentioned above. The
singleton a design pattern whereby only
one instance of a class can ever be cre-
ated, and is useful for handling log files
and other resources where it doesn’t
make sense to have two.
Indirect referencing
Allows
$foo->a->b;
in some cases
Automagic class loading
Use
__autoload
to bring in appropriate files
Setters and Getters
Overload member variable access with
__get
and
__set
Runtime method overload
__call
will be invoked if the original function doesn’t exist
Abstract classes and interfaces
For more complex OO code
Exceptions
Exceptions can be caught and thrown as per other languages
SimpleXML
Supports loading and saving of well formed XML
SQLite
An available database without running extra services. Not recommended for high
loads
01 class Singleton
02 {
03 static private $instance
U
= NULL;
Filters
Flexible text processing, with built in filters for rot13
Streams
To load data resources from arbitrary locations, using various protocols (e.g. ftp, http).
Improved socket handling too
04
05 private function
U
__construct() { }
Namespaces
These have been removed!
Extensions
Various extensions for SOAP, SimpleXML and MySQL.
68
July 2004
www.linux-magazine.com
Plik z chomika:
Kapy97
Inne pliki z tego folderu:
2010.05_Dialog-Adding Gui Elements to Your Scripts with Dialog and Xdialog.pdf
(719 KB)
2010.04_Synthesis-Xml in C++ with Codesynthesis Xsd.pdf
(548 KB)
2010.04_Animated-Test Driving Some New Features of Kde 4.4.pdf
(487 KB)
2010.03_Office Nap Wakeup-Configure Pidgin to Play Personal Sounds.pdf
(613 KB)
2010.02_Sand Game-Strategies for Animating Coarse-Grained Materials.pdf
(630 KB)
Inne foldery tego chomika:
Beginners
Business News
Comment
Community
Community Notebook
Zgłoś jeśli
naruszono regulamin