Wrox Programmer Forums
Go Back   Wrox Programmer Forums > PHP/MySQL > BOOK: Professional PHP6
|
BOOK: Professional PHP6
This is the forum to discuss the Wrox book Professional PHP6 by Edward Lecky-Thompson, Steven Nowicki; ISBN: 9780470395097
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Professional PHP6 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old April 25th, 2013, 08:54 AM
Authorized User
 
Join Date: Sep 2012
Posts: 13
Thanks: 3
Thanked 1 Time in 1 Post
Default Chapter 7 - Object Relation Mapping

So I am finally able to start working on the code for the object relational mapping. While working through that code I noticed that in the User::Save method there is a conditional that never will be evaluated.

Below is the code that I am referring to ( see 164-165 of the English version of the book ).

PHP Code:
// ....

eval('$actualVal = &$this->' $value ';');

if (isset(
$actualVal)) {
     
    if ((
is_int($actualVal)) || ($actualVal == NULL)) {
        
// other code....
    
}
}

// .... 
The second condition of $actualVal == NULL will never evaluate to true. This is because of the preceding isset() method call.

This is what the manual says about the isset() method.

http://us3.php.net/isset

So effectively it is extra code....

Please let me know if I am wrong or misunderstanding the code.
 
Old April 25th, 2013, 09:15 AM
Friend of Wrox
 
Join Date: May 2011
Posts: 125
Thanks: 0
Thanked 24 Times in 24 Posts
Default

Greetings,

Correct. The above "should" be like this;
Code:
eval('$actualVal = &$this->' . $value . ';');

if (isset($actualVal) || ($actualVal == NULL)) {
     
    if ((is_int($actualVal)) || ($actualVal == NULL)) {
        // other code....
    }
}
Assuming of course that 'null' is a possible value of '$actualVal'.

But then 'null' is a pain, but rather helpfull in a db.
The Following User Says Thank You to UseLess For This Useful Post:
ericrjones1 (April 25th, 2013)
 
Old April 25th, 2013, 09:29 AM
Registered User
 
Join Date: Apr 2013
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Why is "eval" even used in this book? I have always heard that you can't rely on "eval" because some hosting companies prevent its use.

Ever heard the term throwing out the baby with the bath water.
 
Old April 25th, 2013, 05:45 PM
Authorized User
 
Join Date: Sep 2012
Posts: 13
Thanks: 3
Thanked 1 Time in 1 Post
Default

Quote:
Originally Posted by UseLess View Post
Greetings,

Correct. The above "should" be like this;
Code:
eval('$actualVal = &$this->' . $value . ';');

if (isset($actualVal) || ($actualVal == NULL)) {
     
    if ((is_int($actualVal)) || ($actualVal == NULL)) {
        // other code....
    }
}
Assuming of course that 'null' is a possible value of '$actualVal'.

But then 'null' is a pain, but rather helpfull in a db.
Thanks for the reply, but now the first conditional statement doesn't make sense. To me, the first conditional statement is saying is the variable $actualVar isset (aka is it in PHP's memory stack and not null) or is it null.

So thinking about this more, maybe the following would work better. It reduces the nested if statements ( a possible source of increased code complexity ), and attempts to identify if $actualValue is null ( which PDO has a NULL parameter constant ).

PHP Code:

if (!isset($actualVal) || is_null($actualVal)) {
    
// set value to null in the database using PDO::PARAM_NULL
} elseif (is_int($actualVal)) {
   
// set value to int in the database using PDO::PARAM_INT
} else {
    
// set the value to string in the database PDO::PARAM_STR

Let me know what you think.





Similar Threads
Thread Thread Starter Forum Replies Last Post
Chapter 14 : Mapping data model to an object model robochrish BOOK: Beginning ASP.NET 4 : in C# and VB 8 March 5th, 2013 09:52 AM
Problem: Chapter 8 "Mapping Earthquakes Example" (Page 282) DrGaribaldi BOOK: Professional Android 2 Application Development 3 July 29th, 2010 06:24 PM
constraint for 1-to-n relation j_yan MySQL 0 December 10th, 2004 11:03 PM
Chapter 5-Image Mapping Help! maxcain Dreamweaver (all versions) 3 March 16th, 2004 09:00 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.