PHP Object-Oriented Solutions

By David Powers

PHP Object-Oriented Solutions Cover Image

A sequel to the best-selling PHP Solutions, this time with an focus on object-oriented coding practices.

Full Description

  • ISBN13: 978-1-4302-1011-5
  • User Level: Beginner to Advanced
  • Publication Date: August 20, 2008
  • Available eBook Formats: PDF
  • Print Book Price: $36.99
  • eBook Price: $25.99
Buy eBook Buy Print Book Add to Wishlist

Related Titles

Full Description

With the surge of popularity of PHP 5, object-oriented programming is now an important consideration for PHP developers. This version-neutral book is a gentle introduction to object-oriented programming (OOP) that won't overburden you with complex theory. It teaches you the essential basics of OOP that you'll need to know before moving onto a more advanced level, and includes a series of prepackaged scripts that you can incorporate into your existing sites with the minimum of effort.

It shows how object-oriented programming can be used to create reusable and portable code by walking you through a series of simple projects. The projects feature the sorts of things developers run up against every day, and include a validator for filtering user input, a simple Date class that avoids the need to remember all the esoteric format codes in PHP, and an XML generator.

  • Teaches the fundamentals of OOP
  • Simple projects show how OOP concepts work in the real world
  • Prepackaged scripts can easily be added to your own projects

What you’ll learn

  • PHP features, such as the Standard PHP Library (SPL), that are poorly documented or ignored by existing books
  • How to develop classes of their own
  • OOP in easy-to-understand language without getting bogged down in dense theory
  • Solid foundations for developers wishing to delve more deeply into OOP
  • How to leverage the strengths of OOP as a means for creating reusable code that can be used successfully within a procedural context
  • How to code for both PHP 5 and 6

Who this book is for

This book is aimed at intermediate-level PHP developers who want to take their skills to the next level by exploring object-oriented programming as a way of creating reusable code for a everyday website tasks. A typical reader would be someone who has read one of the author's previous titles, such as PHP Solutions, or one of the PHP books for Dreamweaver and feels ready to tackle something a little more challenging.

Source Code/Downloads

Downloads are available to accompany this book.

Your operating system can likely extract zipped downloads automatically, but you may require software such as WinZip for PC, or StuffIt on a Mac.

Errata

Please Login to submit errata.

On page 30:
set_ini('display_errors', 1) ;
should be
ini_set(('display_errors', 1) ;

On page 84:

Resetting timezone using htaccess file

the instructions for adding the line

php_value date.timezone 'Europe/London'

don't work for all server set ups. Some times you need to use:

SetEnv TZ 'Europe/London'

This works on my hosting where php is an apache module. I think the line you have given is specific to php running as a CGI module on the server.

On page 95:
missing full stop from the end of the first Exception message (inconsistent with other Exception messages in the text)

On page 105:
addDays(), subDays(), addWeeks() and subWeeks() all fail to update the protected attributes Pos_Date::$_day, $_month and $_year so these become out of sync with the timestamp which is updated via parent::modify()

Example:
$dtzTokyo = new DateTimeZone('Asia/Tokyo');
$Tokyo = new pos_Date($dtzTokyo);
echo '<p>Tokyo time: ' . $Tokyo->format('F jS, Y h:i A') . '</p>';
$Tokyo->addDays(700);
echo '<p>added 700 days to Tokyo time: ' . $Tokyo->format('F jS, Y h:i A') . '</p>';
$Tokyo->addMonths(3);
echo '<p>added 3 months to Tokyo time: ' . $Tokyo->format('F jS, Y h:i A') . '</p>';

Output::
Tokyo time: October 23rd, 2011 11:18 PM
added 700 days to Tokyo time: September 22nd, 2013 11:18 PM
added 3 months to Tokyo time: January 23rd, 2012 11:18 PM

Observation1:
the formatted output for yr month and day no longer matches the attributes.
Observation2:
addMonths does it correctly: it does modify the attributes and then passes those values on to the timestamp via parent::setDate()

On page 105:
re the 4 inadequate add/sub functns ...
add the following to each of them and all's well:
$this->_day = intval($this->format('j'));
$this->_month = intval($this->format('m'));
$this->_year = intval($this->format('Y'));

On page 123:

The URL http://pecl4win.php.net/ext.php/php_filter.dll returns a 404 error as do pecl4win.php.net/ext.php/ and pecl4win.php.net/

Just thought you'd want to know. I've been looking for a Windows version of php_uploadprogress.dll and have been unable to find one that matches my version (5.3.5) under V6 or V9 (not sure which, atm).

Unfortunately, the URL was one I hadn't heard of, so my hopes got dashed again... *sigh*

On page 145, 146:
After modifying the code in test_validator(_01) and Validator.php, the added print_r function displays nothing. Even when I tried test_validator_02. Examples further on in the chapter did not work either. Page 156, test_validator_03 threw this error " Cannot access protected property Pos_Validator::$_filterArgs in Applications/MAMP/htdocs/oopsolutions/ch4_exercises/test_validator_03.php on line 12"
My MAMP environment is PHP 5.3.2, filtering is enabled, It is $Revision294106$ Filer default is unsafe.raw.
Does this class still work? I think these errors would have emerged when the code was published. Has PHP changed filtering? If so, is there an update to the class that incorporates these changes?
I was excited to learn this but now I am uncertain about moving forward with the results I am getting. Please help me work through these issues. Thank you.

On page 292 and 299:

Generating XML from database.

page 292

Multiple authors can be stored in one entry in for instance this way : author1####author2####author3####author4





page 299

And accessed with a few lines of extra code when accessing the results

$result=$conn->getResultSet('SELECT * FROM blog');
foreach($result as $row){
foreach($row as $field =>$value){
$value=explode('####',$value)
foreach($value as $single_value)
echo "$field: $single_valuea<br />";

}
echo "<br/>"
}


And a few lines of code when storing data in the database data."####" - remember exception for the first data (not "####" only data)
to have the right array when exploding.


This adds much flexibility to the class without complex structure.