Page 1 of 1

Namespaces

Posted: Mon Mar 06, 2006 9:09 pm
by neophyte
According to the Zend cert study guide a namespace is a class with methods that are used statically.

Code: Select all

class validate(){
   function is_number($num){
      return is_numeric($num);
   }
}
//used like this
validate::is_number($num);
The study guide says that the advantage of avoiding naming conflicts in a library isn't enough to justify the overhead assoicated with using a class this way. So my question is what is the overhead? Do you agree or disagree? What do you think is a justifiable use for making a class like this?

Posted: Mon Mar 06, 2006 9:25 pm
by feyd
if there's any remote possibility that the function you are creating may already exist in the user's code (a given with a lot of code), a namespace helps a lot. I use namespaces in C a lot, but haven't used them too often in PHP. Although soon I will as projects I'm working on will be used by many others (as you may know.) Anyways, the performance hit is that it is a class, since PHP isn't natively an OOP system, there are tiny performance hits each time a class is interacted with, although fairly minimal, they do add up.

Posted: Mon Mar 06, 2006 10:31 pm
by neophyte
Thanks for the explanation Feyd.

Re: Namespaces

Posted: Tue Mar 07, 2006 4:13 am
by Christopher
neophyte wrote:According to the Zend cert study guide a namespace is a class with methods that are used statically.
A "class with methods" is not a namespace in the true sense of the word. The class constuct can be used to create a namespace. However, there is really no namespacing difference between:

Code: Select all

class validate(){
   function is_number($num){
      return is_numeric($num);
   }
}
validate::is_number($num); 

//AND

function validate_is_number($num){
   return is_numeric($num);
}
validate_is_number($num);
Both are namespaced in the same sense.

Posted: Tue Mar 07, 2006 10:59 am
by neophyte
Here's my attempt at measuring a performance difference: Functions vs. Static Methods.

Code: Select all

<?php
error_reporting(E_ALL);
class namespace {
    function isNumber($val)
    {
        return is_numeric($val);

    }
    function isString($val)
    {
      return is_string($val);

    }
    function isBoolean($val)
    {
        return is_bool($val);
    }
}
//Functions
  function isNumber($val)
    {
        return is_numeric($val);
    }
    function isString($val)
    {
        return is_string($val);
    }
    function isBoolean($val)
    {
        return is_bool($val);
    }
function microtime_float()
{
   list($usec, $sec) = explode(" ", microtime());
   return ((float)$usec + (float)$sec);
}
$firstMicro = microtime_float();
for($x=0; $x<100000; $x++){
	isNumber(1);
	isString('string');
	isBoolean(true);
}
$secondMicro = microtime_float();
for($x=0; $x<100000; $x++){
	namespace::isNumber(1);
	namespace::isString('string');
	namespace::isBoolean(true);
}
$thirdMicro = microtime_float();
echo "functions: ".($secondMicro - $firstMicro)."\n\r";
echo "namespace: ".($thirdMicro - $secondMicro)."\n\r";

?>
Here is the output:
functions: 0.49018406867981
namespace: 0.60841798782349
Comments?

Posted: Tue Mar 07, 2006 8:33 pm
by Gambler
PHP seems to do 2 hash lookups every time class function is invoked. Honestly, sometimes I get the feeling that writing my own include function and using tokenizer to optimize stuff would be a good idea.

Posted: Tue Mar 07, 2006 9:33 pm
by Christopher
On what version of PHP was that benchmark run? Was that the average of many runs? Did you time it with the order of the tests reversed? Those numbers look a lot closer than I remember. I seems like unless you are writing a "loop a million times an call a function each iteration" program, the difference trivial.