PHP Object-Oriented Solutions: Corrections
This page lists known errors in PHP Object-Oriented Solutions, published by friends of ED in August 2008.
Page 30: In the highlighted section at the end of step 7, set_ini('display_errors', '1') should be ini_set('display_errors', '1').
Page 45: The keyword class has been omitted from the first line of the block of code at the top of the page. It should be as follows:
final class Ch2_Book extends Ch2_Product
{
// class definition omitted
}
Page 90: In PHP 5.3 and later, overriding the DateTime class's modify() method generates a strict error. To avoid doing so, the updated version of the download files no longer blocks its use.
Page 97: Delete step 5 (see explanation above).
Page 99: The setMDY() and setDMY() methods do not check that the year has been submitted as four digits. To improve them, add an elseif clause as shown here:
public function setMDY($USDate)
{
$dateParts = preg_split ( '{[-/ :.]}', $USDate );
if (! is_array ( $dateParts ) || count ( $dateParts ) != 3) {
throw new Exception ( 'setMDY() expects a date as "MM/DD/YYYY".' );
} elseif (strlen($dateParts[2]) != 4) {
throw new Exception ( 'setMDY() expects the year as four digits.' );
}
$this->setDate ( $dateParts [2], $dateParts [0], $dateParts [1] );
}
public function setDMY($EuroDate)
{
$dateParts = preg_split ( '{[-/ :.]}', $EuroDate );
if (! is_array ( $dateParts ) || count ( $dateParts ) != 3) {
throw new Exception ( 'setDMY() expects a date as "DD/MM/YYYY".' );
} elseif (strlen($dateParts[2]) != 4) {
throw new Exception ( 'setDMY() expects the year as four digits.' );
}
$this->setDate ( $dateParts [2], $dateParts [1], $dateParts [0] );
}
Page 105: The addDays(), subDays(), addWeeks(), and subWeeks() methods need to update the protected $_year, $_month, and $_day properties, as shown in this revised version of addDays():
public function addDays($numDays)
{
if (! is_numeric ( $numDays ) || $numDays < 1) {
throw new Exception ( 'addDays() expects a positive integer.' );
}
parent::modify ( '+' . intval ( $numDays ) . ' days' );
$this->_year = ( int ) $this->format ( 'Y' );
$this->_month = ( int ) $this->format ( 'n' );
$this->_day = ( int ) $this->format ( 'j' );
}
The same code needs to be added to each method.
Page 132: Delete the final paragraph and code example at the bottom of the page. Replace with the following: "To filter user input from a form, use filter_input(), which takes INPUT_POST or INPUT_GET as its first argument. To check that $_POST['var'] or $_GET['var'] contains an integer, use the following code, as appropriate:"
$filtered = filter_input(INPUT_POST, 'var', FILTER_VALIDATE_INT);
$filtered = filter_input(INPUT_GET, 'var', FILTER_VALIDATE_INT);
Pages 154–155: In the checkTextLength() method, all four instances of $this->errors[] should be $this->errors[$fieldName].







