Provided that in the previous section you learned how to link a basic "User" class to a specific namespace, the next thing that I'm going to show you in this tutorial will be how to call an instance of that class within a PHP script. The following script demonstrates in a simple way how to work with a "$user" object whose originating class has been tied previously to a "UserManager::CMS" namespace. Have a look at it, please: <?php namespace UserManager::CMS;
class User{ private $firstName; private $lastName; private $email; public function __construct($firstName,$lastName,$email){ if(!$firstName||strlen($firstName)>32){ throw new Exception('Invalid First Name parameter!'); } if(!$lastName||strlen($lastName)>32){ throw new Exception('Invalid Last Name parameter!'); } if(!$email||!preg_match("/^.+@.+..+$/",$email)){ throw new Exception('Invalid Email parameter!'); } $this->firstName=$firstName; $this->lastName=$lastName; $this->email=$email; } // get user's first name public function getFirstName(){ return $this->firstName; } // get user's last name public function getLastName(){ return $this->lastName; } // get user's email public function getEmail(){ return $this->email; } } try{ // create new instance of 'User' class by using the specified namespace $user=new UserManager::CMS::User('Alejandro','Gervasio','alejandro@domain.com'); // display user data echo 'First Name: '.$user->getFirstName().'<br />'; echo 'Last Name: '.$user->getLastName().'<br />'; echo 'Email: '.$user->getEmail().'<br />'; } catch(Exception $e){ echo $e->getMessage(); exit(); } ?> Definitely, the most important thing to note here is how an instance of the "User" class is called within the previous script. The following statement: $user=new UserManager::CMS::User('Alejandro','Gervasio','alejandro@domain.com'); creates a new $user object, where the name of the class is preceded by the corresponding namespace. Also, you should note the use of the (::) operator, which indicates that the class in question is linked to the "UserManager::CMS" namespace. In this case, I used the "namespace" keyword to associate a sample class to a given namespace, which can be longer to code. There are a few additional methods in PHP 5 that permit you to perform the same task, but they'll be properly reviewed in upcoming tutorials of the series. For now, study the code samples developed previously to acquire a more solid background in using namespaces in PHP 5. And keep in mind that this feature will be available hopefully with the release of PHP 5.3. Final thoughts In this first part of the series, I provided you with a quick introduction to using namespaces in PHP 5. As you saw for yourself, using this feature is indeed a straightforward process that can be grasped with minor hassles. In the next tutorial, I'm going to show you how to work with two classes that share the same name, but are linked to different namespaces. Now that you're aware of the subject of this forthcoming article, you won't want to miss it!
blog comments powered by Disqus |
|
|
|
|
|
|
|