Namespace Question

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
thomas777neo
Forum Contributor
Posts: 214
Joined: Mon Mar 10, 2003 6:12 am
Location: Johannesburg,South Africa

Namespace Question

Post by thomas777neo »

Good Day

I am messing around with the new stuff in the php5.3.0 release, specifically namespaces.

namespace.php

Code: Select all

<?php
namespace Pro7\Amphibian;
 
class evolution
{
    function connect($dbConn)
    {
        return $dbConn;
    }
}
 
$evolution = new evolution; // za\co\pro7\amphibian\evolution
 
var_dump($evolution);
 
$namespace = __NAMESPACE__;
echo "\n\$namespace:$namespace\n";
 
$connect = $evolution-> connect("mysql");
echo "\$connect:$connect\n";
?>
useNamespace.php

Code: Select all

 
<?php
use Pro7\Amphibian\evolution as evolution;
 
$obj = new evolution;
?>
 
I am getting an error in useNamespace.php when trying to create a new object of the class in my namespace.
C:\php5.3.0>php useNamespace.php
PHP Fatal error: Class 'Pro7\Amphibian\evolution' not found in C:\php5.3.0\useN
amespace.php on line 4
I am not sure what I am doing incorrectly. I think that my concept of the global availability of the namespace might be incorrect.
User avatar
thomas777neo
Forum Contributor
Posts: 214
Joined: Mon Mar 10, 2003 6:12 am
Location: Johannesburg,South Africa

Re: Namespace Question

Post by thomas777neo »

Thanks for your feedback.

Well, it is unfortunate that you need to include the namespace file :banghead:

As it does defeat the point a bit.
Post Reply