Namespaces were created as part of PHP 5.3 to solve certain conflicts that can arise when two different classes are given the same name. This situation comes up more commonly than you might think. This four-part series introduces you to using namespaces to solve these and similar issues. It includes copious code examples to enhance your understanding.
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
&lt;/p&gt;<br /> &lt;p style=&quot;text-align: justify;&quot;&gt;&lt;?php&lt;/p&gt;<br /> &lt;p style=&quot;text-align: justify;&quot;&gt;<br /> namespace UserManager::CMS;&lt;/p&gt;<br /> &lt;p style=&quot;text-align: justify;&quot;&gt;&lt;/p&gt;<br /> &lt;p style=&quot;text-align: justify;&quot;&gt;class User{&lt;/p&gt;<br /> &lt;p style=&quot;text-align: justify;&quot;&gt;private $firstName;&lt;/p&gt;<br /> &lt;p style=&quot;text-align: justify;&quot;&gt;private $lastName;&lt;/p&gt;<br /> &lt;p style=&quot;text-align: justify;&quot;&gt;private $email;&lt;/p&gt;<br /> &lt;p style=&quot;text-align: justify;&quot;&gt;public function __construct($firstName,$lastName,$email){&lt;/p&gt;<br /> &lt;p style=&quot;text-align: justify;&quot;&gt;if(!$firstName||strlen($firstName)&gt;32){&lt;/p&gt;<br /> &lt;p style=&quot;text-align: justify;&quot;&gt;throw new Exception('Invalid First Name parameter!');&lt;/p&gt;<br /> &lt;p style=&quot;text-align: justify;&quot;&gt;}&lt;/p&gt;<br /> &lt;p style=&quot;text-align: justify;&quot;&gt;if(!$lastName||strlen($lastName)&gt;32){&lt;/p&gt;<br /> &lt;p style=&quot;text-align: justify;&quot;&gt;throw new Exception('Invalid Last Name parameter!');&lt;/p&gt;<br /> &lt;p style=&quot;text-align: justify;&quot;&gt;}&lt;/p&gt;<br /> &lt;p style=&quot;text-align: justify;&quot;&gt;if(!$email||!preg_match(&quot;/^.+@.+..+$/&quot;,$email)){&lt;/p&gt;<br /> &lt;p style=&quot;text-align: justify;&quot;&gt;throw new Exception('Invalid Email parameter!');&lt;/p&gt;<br /> &lt;p style=&quot;text-align: justify;&quot;&gt;}&lt;/p&gt;<br /> &lt;p style=&quot;text-align: justify;&quot;&gt;$this-&gt;firstName=$firstName;&lt;/p&gt;<br /> &lt;p style=&quot;text-align: justify;&quot;&gt;$this-&gt;lastName=$lastName;&lt;/p&gt;<br /> &lt;p style=&quot;text-align: justify;&quot;&gt;$this-&gt;email=$email;&lt;/p&gt;<br /> &lt;p style=&quot;text-align: justify;&quot;&gt;}&lt;/p&gt;<br /> &lt;p style=&quot;text-align: justify;&quot;&gt;// get user's first name&lt;/p&gt;<br /> &lt;p style=&quot;text-align: justify;&quot;&gt;public function getFirstName(){&lt;/p&gt;<br /> &lt;p style=&quot;text-align: justify;&quot;&gt;return $this-&gt;firstName;&lt;/p&gt;<br /> &lt;p style=&quot;text-align: justify;&quot;&gt;}&lt;/p&gt;<br /> &lt;p style=&quot;text-align: justify;&quot;&gt;// get user's last name&lt;/p&gt;<br /> &lt;p style=&quot;text-align: justify;&quot;&gt;public function getLastName(){&lt;/p&gt;<br /> &lt;p style=&quot;text-align: justify;&quot;&gt;return $this-&gt;lastName;&lt;/p&gt;<br /> &lt;p style=&quot;text-align: justify;&quot;&gt;}&lt;/p&gt;<br /> &lt;p style=&quot;text-align: justify;&quot;&gt;// get user's email&lt;/p&gt;<br /> &lt;p style=&quot;text-align: justify;&quot;&gt;public function getEmail(){&lt;/p&gt;<br /> &lt;p style=&quot;text-align: justify;&quot;&gt;return $this-&gt;email;&lt;/p&gt;<br /> &lt;p style=&quot;text-align: justify;&quot;&gt;}&lt;/p&gt;<br /> &lt;p style=&quot;text-align: justify;&quot;&gt;}&lt;/p&gt;<br /> &lt;p style=&quot;text-align: justify;&quot;&gt;<br /> try{&lt;/p&gt;<br /> &lt;p style=&quot;text-align: justify;&quot;&gt;// create new instance of 'User' class by using the specified namespace&lt;/p&gt;<br /> &lt;p style=&quot;text-align: justify;&quot;&gt;$user=new UserManager::CMS::User('Alejandro','Gervasio','alejandro@domain.com');&lt;/p&gt;<br /> &lt;p style=&quot;text-align: justify;&quot;&gt;// display user data&lt;/p&gt;<br /> &lt;p style=&quot;text-align: justify;&quot;&gt;echo 'First Name: '.$user-&gt;getFirstName().'&lt;br /&gt;';&lt;/p&gt;<br /> &lt;p style=&quot;text-align: justify;&quot;&gt;echo 'Last Name: '.$user-&gt;getLastName().'&lt;br /&gt;';&lt;/p&gt;<br /> &lt;p style=&quot;text-align: justify;&quot;&gt;echo 'Email: '.$user-&gt;getEmail().'&lt;br /&gt;';&lt;/p&gt;<br /> &lt;p style=&quot;text-align: justify;&quot;&gt;}&lt;/p&gt;<br /> &lt;p style=&quot;text-align: justify;&quot;&gt;catch(Exception $e){&lt;/p&gt;<br /> &lt;p style=&quot;text-align: justify;&quot;&gt;echo $e-&gt;getMessage();&lt;/p&gt;<br /> &lt;p style=&quot;text-align: justify;&quot;&gt;exit();&lt;/p&gt;<br /> &lt;p style=&quot;text-align: justify;&quot;&gt;}&lt;/p&gt;<br /> &lt;p style=&quot;text-align: justify;&quot;&gt;<br /> ?&gt;<br /> |
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:
1 |
$user=new UserManager::CMS::User('Saidur','Rahman','ranaphp@yahoo.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.