Using classes as namespaces: good style?

From: Christian Hackl (hacki_at_sbox.tugraz.at)
Date: 03/04/05


Date: Fri, 04 Mar 2005 22:04:13 +0100

Is it considered good style to use classes as namespaces in PHP (that
is, PHP 4.3.1)?

Like:

class MyClass
{
  function test() { echo 'MyClass::test'; }
}

class YourClass
{
  function test() { echo 'YourClass::test'; }
}

MyClass::test();
YourClass::test();

Reason is: I've got several functions that do almost the same thing, yet
giving them all different names may result in long, ugly, and
error-prone names like getArticleUpperRowUsingHeading.

Using proper parameters like getArticleRow($side, $using_heading) is
also out of question, because I wish to use the functions as variable
functions (call_user_func). The custom arguments (they'd all differ from
function to function) logically wouldn't even be known by the caller.

Putting the functions into classes looks like an acceptable alternative
to me, though doing so may actually be very stupid... I don't know which
way to go :)

-- 
Christian Hackl


Relevant Pages

  • Re: Including file Inside class
    ... <?php echo $foo; ?> ... It has something to do with variable scope and accessing globals or ...
    (comp.lang.php)
  • Re: copy of object is a reference in PHP5
    ... myObj Object ... All objects in PHP 5, if I'm not mistaken, are always passed by ... class MyClass ... $class1 = new MyClass; ...
    (comp.lang.php)
  • Re: what is the -> operator?
    ... PHP manual at php.net, I can't find a description of what it does. ... class foo ... If I have a class myClass with methods foo and bar ...
    (comp.lang.php)
  • Re: copy of object is a reference in PHP5
    ... All objects in PHP 5, if I'm not mistaken, are always passed by ... class MyClass ... $class1 = new MyClass; ... What I have done is create objects with numerous properties ...
    (comp.lang.php)
  • Re: Making groups
    ... It's actually the native array type in PHP. ... One more reason to avoid PHP. ... I've been doing database stuff since oracle cost $100K a seat. ... They're actually exceedingly useful once you get used to them. ...
    (comp.lang.tcl)