Why is PHP Storm giving me an undefined constant error?

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

burnedfacelessAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Is the part captioned /*sample xml the test data?
0
Ray PaseurCommented:
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
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
burnedfacelessAuthor Commented:
Thanks Ray
0
Ray PaseurCommented:
10-4.  Hope it helped!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.