Home arrow PHP arrow Page 4 - Using Namespaces in PHP 5

Working with a PHP class tied to a specific namespace - PHP

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.

TABLE OF CONTENTS:
  1. Using Namespaces in PHP 5
  2. The need to use namespaces in PHP 5
  3. Tying a PHP class to a namespace with the namespace keyword
  4. Working with a PHP class tied to a specific namespace
By: Alejandro Gervasio
Rating: starstarstarstarstar / 9
November 12, 2008

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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!



 
 
>>> More PHP Articles          >>> More By Alejandro Gervasio
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: