• 6
name

A PHP Error was encountered

Severity: Notice

Message: Undefined index: userid

Filename: views/question.php

Line Number: 191

Backtrace:

File: /home/prodcxja/public_html/questions/application/views/question.php
Line: 191
Function: _error_handler

File: /home/prodcxja/public_html/questions/application/controllers/Questions.php
Line: 433
Function: view

File: /home/prodcxja/public_html/questions/index.php
Line: 315
Function: require_once

name Punditsdkoslkdosdkoskdo

To PHP Namespace or not to PHP Namespace [closed]

Here are some solution attempts for those wondering about name space functionality, without php 5.3.

First, I must say, only prefixing worked for me, the other solutions below are sort of notes not to go that way.

Namespace functionality can be needed if, for example, one produces wordpress plugins, using your valued function library in them. Normally you cannot have multiple plugins with function declarations of same functions, it would cause fatal errors.

SOLUTION 1: prefix all your library functions with plugin_name_ or namespace_

 function str_len_utf8($str)

becomes

 function photoplugin_str_len_utf8($str)

Is just a matter of find-replace. Easy if the functions are already prefixed uniquely:

john_str_len_utf8() => photoplugin_john_str_len_utf8()

Good for the soul as well, supports the ego, 'johns string library' :D

If you choose short and nice prefixes with some common sense, it works like a charm, as they say.

SOLUTION 2: enclose all your library/reeuse functions in a class.

class photopluginlib
     {
     function str_len_utf8($a){ $a=$this->str_clean_utf8($a); ...}
     ...
     }

All function calls in the class to the class are prefixed with $this->. Once this class is prepared, one can use it repeatedly without search-replace, changing the classname is enough. Usage:

$photopluginlib=new photopluginlibrary();
$b=$photopluginlib->str_len_utf8($a);

SOLUTION 3: enclose all your library/reeuse functions in a class, and use :: operator

class photopluginlib
     {
     static function str_len_utf8($a){ $a=self::str_clean_utf8($a); ...}
     ...
     }

All function declarations in the class have keyword static in front of function.

All function calls inside the class to the class are prefixed with self::

Once this class is prepared, one can use it without search-replace. The class name is the namespace, sort of. One only changes the class name and uses it as such.

$b=photopluginlib::str_len_utf8($a);
$c=photopluginlib::database_row(...)

No need to instantiate the class.

Looks nicer than $photopluginlib->str_len_utf8(), but I still prefer photoplugin_john_str_len_utf8()

Notes

  • all libraries need to go into 1 big library class declaration, you practically cannot add methods to classes later in php.
  • you cannot just declare new library functions here and there in different php files.
  • if multiple libraries are used, and they are using each other, they need to use self:: for function calls.
  • php5.2.17, common 2013, does not accept call_user_func('photopluginlib::functionname'), need to use call_user_func(Array('photopluginlib','functionname')) or inside class, call_user_func(Array(__CLASS__,'functionname')), which means code rewrite for wp add_action, preg_replace_callback etc.
  • needs php>=5
  • cannot mix code and function declarations, as in:

class photopluginlib{ add_filter('html','myfilter'); static function myfilter($html){return ''} }

  • For big code, it can quickly become a big complicated mind bender.

I prefer to just use prefixing until the real thing, namespaces, are widely available.

These solutions still mean one has to prefix all function uses. Namespace functionality would mean one can use functions without prefixing them, the prefix would be just once, at begin of the php file:

<?php
namespace photoplugin;
...
$b=str_len_utf8($a);

By the way, upgrading to php5.3 has an additional advantage, if you can choose not to code for php5.2 (still everywhere, year 2013) Php5.3 vs php 5.2.17 means instant speed increase. It looks like %30 etc speed increases are available, not considering the database side:

http://onlinephpfunctions.com/benchmarks

Hope this helps inspire some solutions to those reaching for namespaces.

  • 6
Reply Report

Its use is already catching on. A couple of projects use it in their upcoming/beta versions. Most examples I've seen however use it like a cargo cult. Doctrine2 for example uses five or more nested namespaces (code smell), probably to provide a 1:1 mapping of namespace/class to the filesystem/directories. I guess the novelty makes PHP namespaces prone to unreasoned overuse.

Anyway, the syntax doesn't help with readability that much. And it's a big turn off for professional programmers. But if there is a serious use case in your project, just go for it. (Hypothetical naming conflicts are not the best reason.)

  • 5
Reply Report

They will most likely not catch on until the core starts using them (in PHP 7 maybe possibly perhaps...), but using Python for a few months will show you that namespaces are AWESOME.

  • 3
Reply Report
      • 1
    • I don't think it will take that long. The major frameworks are already transitioning to namespaces. I'd say the main argument against namespaces right now (besides the backslash being dirt ugly) is the lacking availability of PHP 5.3 on shared hosts.
      • 2
    • @Gordon I agree with the availability of 5.3 on shared hosts - its pretty damm annoying and yes the backslash is really ugly for some reason. I have been trying to think why its so ugly but I cant seem to think exactly why - its really bad for some reason.
      • 1
    • @David: It's already used in strings to give special meaning to some characters, or to escape quotes. We've had to double them for so long that seeing a single backslash offends our sensibilities.
      • 1
    • yes i see where you are comming from - could it possibly be though that it so different to all the other c based languages?

Unless all your code runs on your own servers, it's too early to start using them as 5.3 is relatively new.

Other than that, I'm not sure if they will ever really catch on. Even classes took a long time to catch on with a large portion of the PHP programming population.

  • 3
Reply Report
      • 2
    • I'm not sure yet. I haven't played with it enough. In theory though, I'd say yes since PHP desperately needs namespaces to stop the proliferation of unreadably long function names.

I would start learning how to use namespaces as soon as possible. Zend Framework 2.0 will use namespaces, which will mean that anyone using PHP 5.2 or lower will be out of luck. I use a virtual dedicated server, so I can control my PHP version. If you use cPanel/WHM, you can install PHP 5.3 very easily. If you are on shared hosting, it may be a little bit before you see 5.3 installed, although there are 5.3 adopters out there.

  • 1
Reply Report

Namespace is not needing for PHP.

For example, in C# and Java, it is usual to use several libraries (specially standard one) and each libraries contain thousand of classes and definitions for each library. So, the chance of a conflict is high.

Instead, in PHP is different, we don't need to load several libraries, at most a framework plus some separates .php files. How PHP works discourage to loads biggest libraries but to use a bare minimum. It is why the chance of a name conflict is pretty dim. Also, frameworks are seasoned to use a short prefix, for example Wp_User (Wordpress).

  • 0
Reply Report

Warm tip !!!

This article is reproduced from Stack Exchange / Stack Overflow, please click

Trending Tags

Related Questions