The class looks like this:
class do{
function do_something($what){
...
echo $what;
}
function do_other_stuff($what){
...
echo $what;
}
}
and I would use it like if(do::do_something('whatever'))...
or do::do_other_stuff('whatever')...
Is there a point using a class like this?
basically I'm just using its functions just like I开发者_如何学运维 would use them if the functions are outside the class.
If you're just calling functions of a class as if they are procedural functions, then no, I wouldn't say there's much value to it. All it does then is serve to help you group your methods together but that could be accomplished via prefixing, etc. The value of OO programming comes about through the use of encapsulation, separation of concerns, etc. and you're not taking advantage of any of that with the code sample you've provided. I think using the OO functionality of PHP can be very helpful, but you have to make sure you fully understand OO programming and are in the OO mindset otherwise you won't take advantage of that functionality and you'll just be writing procedural code anyway.
What you're doing is technically incorrect - if you enable E_STRICT warnings you'll see that PHP is throwing an error along the lines of...
PHP Strict Standards: Non-static method do::do_something() should not be called statically in...
Irrespective, I'd recommend a read of the existing Functions vs. Static Methods question and the PHP Classes and Objects manual section for more information.
it's pointless.
include your function above a namespace if you need something like that
http://www.php.net/manual/en/language.namespaces.basics.php
That's a poor mans namespace. While it's certainly not the recommended way to design OOP classes, it has its uses for assembling utility code. So it's not all bad.
When designing real object structures however, it's best to not think about having mundane tasks done by them, but "message passing". Think about results or occourences when designing methods. Also beware of setter and getter methods. (Read for a nice introduction on meaningful OOP.)
If you do choose to use static methods, you should declare them static. As some have mentioned, the main benefit of the way you are using it is to group similar functions together, I do this for some helper functions sometimes but now that I have learned OO, procedural coding is just not the way to go anymore, I highly recommend learning more about OO coding, you can really do some amazing things with it.
class do{
public static function do_something($what){
...
echo $what;
}
public static function do_other_stuff($what){
...
echo $what;
}
}
精彩评论