Why is PHP Storm giving me an undefined constant error?

burnedfaceless
burnedfaceless used Ask the Experts™
on
I am outlining chapter 3 of PHP Objects, Patterns, and Practice. What an incredible book.

PHP Storm is giving me the error of undefined constant simple xml and undefined function load file.

I want to be able to open this file in a browser (I'm doing one file a chapter with comments for notes). I haven't worked with XML and don't understand what is going on here.

Would someone please explain what is going on, but more importantly how to get it to load in a browser? Should I comment that line out?

//listing 03.14                                                                                                      
                                                                                                                     
/*sample xml                                                                                                         
<settings>                                                                                                           
  <resolvedomains>false</resolvedomains>                                                                             
</settings>                                                                                                          
*/                                                                                                                   
                                                                                                                     
//listing 03.15                                                                                                      
                                                                                                                     
class AddressManager                                                                                                 
{                                                                                                                    
    private $addresses = ["209.131.36.159", "216.58.213.174"];                                                       
    public function outputAddresses($resolve) {                                                                      
        foreach ($this->addresses as $address) {                                                                     
            print $address;                                                                                          
            if ($resolve) { //here is PHP making a judgement call to answer "TRUE" in an interaction with a string   
                print " (".gethostbyaddr($address).")";                                                              
            }                                                                                                        
        }                                                                                                            
                                                                                                                     
    }                                                                                                                
}                                                                                                                    
                                                                                                                     
   //listing 03.16                                                                                                   
                                                                                                                     
    $settings = simplexml:load_file(__DIR__."/resolve.xml");                                                         
    $manager = new AddressManager();                                                                                 
    $manager->outputAddresses((string)$settings->resolvedomains);                                                    

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2011
Top Expert 2016

Commented:
I think line 27 should be simplexml_load_file instead of simplexml:load_file.

Is the part captioned /*sample xml the test data?
Most Valuable Expert 2011
Top Expert 2016
Commented:
Now that I've looked at this a little more, I think I see a few things worth discussing.  Here is my version of the code snippet.
<?php // demo/temp_burnedfaceless.php
/**
 * https://www.experts-exchange.com/questions/29009582/Why-is-PHP-Storm-giving-me-an-undefined-constant-error.html
 */
error_reporting(E_ALL);

// listing 03.14

// sample xml
$xml = <<<EOX
<settings>
  <resolvedomains>false</resolvedomains>
</settings>
EOX;


// listing 03.15

class AddressManager
{
    private $addresses = ["209.131.36.159", "216.58.213.174"];
    public function outputAddresses($resolve) {
        foreach ($this->addresses as $address) {
            print $address;
            if ($resolve) { //here is PHP making a judgement call to answer "TRUE" in an interaction with a string
                print " (" . gethostbyaddr($address) . ")";
            }
        }

    }
}

// listing 03.16

$settings = simplexml_load_string($xml);
$manager = new AddressManager();
$manager->outputAddresses((string)$settings->resolvedomains);

Open in new window


Notes:
Line 5 Always raise error reporting to the highest level.  Without it you can miss things.
Line 10 Guessing that this was the test data, I wrapped the settings in a HEREDOC declaration and assigned the string to $xml, so I could use SimpleXML_Load_String() which is functionally equivalent to SimpleXML_Load_File().
Line 35 Loads $xml into an object assigned to $settings
Line 36 Instantiates AddressManager, assigning the object to $manager
Line 37 Calls method outputaddresses() in the $manager object

Line 37 has a lot of moving parts.  Generally we can decipher these by working from right to left.

$settings->resolvedomains is the property of the settings object that contains the word "false"

(string) tells PHP to cast the property to a string value

The resulting string is an argument to $manager->outputAddresses() where it is named $resolve

$resolve contains the character string "false" but this is deceptive because any non-empty string in PHP is loosely cast to boolean TRUE (unless the string contains a zero, then it may be cast to boolean FALSE).  PHP has a whole set of weirdness around its type juggling rules!

As a result, if ($resolve) { uses the boolean TRUE and the if() control structure runs.

Unless you use a schema, XML does not have any boolean TRUE or FALSE, as far as I know.  You can get the FALSE effect by changing the word "false" to zero.  PHP will loosely cast zero to boolean FALSE in the context of the if() evaluation.

Does that help make sense of it?

Some refs:
http://php.net/manual/en/types.comparisons.php
http://php.net/manual/en/language.operators.comparison.php
http://php.net/manual/en/language.types.type-juggling.php
http://php.net/manual/en/language.types.string.php#language.types.string.conversion

Author

Commented:
Thanks Ray
Most Valuable Expert 2011
Top Expert 2016

Commented:
10-4.  Hope it helped!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial