TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Absolute Beginners (http://www.talkphp.com/absolute-beginners/)
-   -   Namespaes? (http://www.talkphp.com/absolute-beginners/1958-namespaes.html)

Aaron 01-15-2008 03:02 AM

Namespaes?
 
I just got the newsletter... what exactly are namespaces?

Wildhoney 01-15-2008 03:30 AM

I'm sure others can give you a better description, but in a nutshell, namespaces are essentially a way in which to cipher of various code into namespaces. Think of a namespace as a category. Therefore anything in that category can have the same function name as another function in a different namespace (or category).

Basically, when you and others develop a PHP script now, you'll see you have to append some text that identifies your script to each class you create for that script, but with namespaces you can place all your classes in a namespace and all the classes within there can be named whatever you like, because they won't conflict with anything else.

Welcome back to TalkPHP by the way :-) It's a pleasure to have you round again!

Haris 01-15-2008 04:13 AM

I still don't get what namespaces are. Any real world examples?

sjaq 01-15-2008 07:44 AM

PHP: Namespaces - Manual ;-)

Aaron 01-15-2008 12:44 PM

Thanks for the welcome O.o.

I didn't think I was gone that long! (I was caught up in school work, and trying to find a good book on MySQL).

So a namespace is something used in OOP?

buggabill 01-15-2008 01:03 PM

Correct me if I am wrong, but from my very limited understanding, it appears to give classes their own level of scope. PHP classes were totally global before. This meant that if you named your class something like getUserName, no one else could in a large project. Namespaces will allow duplicate class names to happen in instances where more than one library in a project might have similar functionality. Before, one would have to append some sort of unique prefix to the class name in order to insure its uniqueness. This could have resulted in very long class names.

sketchMedia 01-18-2008 12:37 AM

namespaces can be used to help you organize your classes. More importantly, namespaces help programmers aviod name clashes and stupidly long class names (if we were to prefix class names to stop clashing) like this:

Zend_Search_Lucene_Analysis_Analyzer_Common_Text_C aseInsensitive

from what i have been reading, php's namespaces will look like this:
PHP Code:


namespace Sketch::DB;

class 
mysql
{
    
//class gubbings
    
public function connect()
    {
        echo 
'hellllllloooooooooo';
    }
}


//to use:

use Sketch::DB::mysql();

//or to make an alias (in this case DBConn):

use Sketch::DB::mysql() as DBConn;

//or just
 
use Sketch::DB
//(its the same as doing use Sketch::DB as DB, but in simple form)



//so you can invoke in he following ways for each of the methods listed above:

$pDb = new Sketch::DB::mysql();
//or
$pDb = new DBConn(); 
//or
$pDb = new DB::mysql(); 



All times are GMT. The time now is 11:20 AM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0