Namespaces

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
User avatar
neophyte
DevNet Resident
Posts: 1537
Joined: Tue Jan 20, 2004 4:58 pm
Location: Minnesota

Namespaces

Post 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?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
User avatar
neophyte
DevNet Resident
Posts: 1537
Joined: Tue Jan 20, 2004 4:58 pm
Location: Minnesota

Post by neophyte »

Thanks for the explanation Feyd.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Namespaces

Post 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.
(#10850)
User avatar
neophyte
DevNet Resident
Posts: 1537
Joined: Tue Jan 20, 2004 4:58 pm
Location: Minnesota

Post 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?
Gambler
Forum Contributor
Posts: 246
Joined: Thu Dec 08, 2005 7:10 pm

Post 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.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post 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.
(#10850)
Post Reply