tags:

views:

185

answers:

5

I have recently learnt about namespaces in PHP, and was wondering, would it be bad practice to do this?

<?php

namespace String; // string helper functions

    function match($string1, $string2)
    {

     $string1 = trim($string1);
     $string2 = trim($string2);

     if (strcasecmp($string1, $string2) == 0) // are equal 
     {
      return true;
     } else {
      return false;
     }
    }


?>

Before I had this as a static method to a String class, but this always seemed wrong to me... Which is the better alternative? I have a desire to categorise my helper functions, and want to know which is the better way to do it.

Edit So how do I then access the match function if I have declared it in a namespace? How do I stop the namespace declaration, say if I include a file beneath it which declared a new namespace, would it automatically stop references to the first? And how do I go about returning to global namespace?

A: 

You would use classes to categorized some sort of functions on a certain dat. Like let say you have a Student class which will hold student information and student age and student ID number;and alose there some sort of methods that return and set these data.

Moreover, you would use namespace to categorized the Classes;let say now you have another class which handle the University registration for those students and alose the other classes for managing courses. Personaly, I would put all these classes under a category (i.e. namespace) called University.

Thus, it is not even a bad practice, also it gives you more readability in your code. And that would be more underestandable for other programmers if you are working in a group.

So personally I strongly suggest you use those in your project wherever you think it is proper to use it.

Pooria
+2  A: 

I'd put it in a namespace. A class is a describes a type in php, and though you maybe operating over a type with a similar name, it's not the same type, strictly speaking. A namespace is just a way to logically group things, which is what you're using a class to do anyways. The only thing you lose is extensibility, but as you said, it's only a collection of static methods.

Saem
+1  A: 

Because namespaces are so new to php, people have been doing for a long time what you did - create a class, add static methods, and use the class as if it were a namespace. It looks to me as if you are thinking about it properly.

le dorfier
So which one is better ?
alex
What - classes or namespaces? They are both appropriate in different cases. If you are enhancing PHP string functionality, I'd use a namespace rather than a class, though, since I'm not sure it makes sense to create a class where php doesn't have a native one.
le dorfier
+2  A: 

If they're all string-related functions, there's nothing wrong with making them static methods.

A namespace would be the better choice if you've got a bunch of global functions and/or variables which you don't want cluttering up global scope, but which wouldn't make sense in a class. (I've got a class in my own code which would be a lot better suited to a namespace).

It's good practice to stick everything in some sort of container if there's any chance your code will get reused elsewhere, so you don't scribble on other people's global vars...

As for your edit there:

  • Namespaces are tied to the file they're defined in, IIRC. Not sure how this affects included files from there but I'd guess they aren't inherited.
  • You can call the global scope by saying ::trim().
Ant P.
Thanks for this answer, very informative. If you think a namespace is better, do you think I should go with it?
alex
If it's not a huge amount of work, just go ahead and do it - after all you can always change it back.
Ant P.
A: 

Ideally, it would be best to put functions in a namespace. If you depend on autoloading though, which only works for classes, you need to put them in one or more classes.

Bart van Heukelom