0

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.

1

5 Answers 5

9

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.

Sign up to request clarification or add additional context in comments.

Comments

4

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.

3 Comments

That doesn't answer the question - that's simply a STRICT syntax error. If he changed it to public static function() { it'd be fine.
@xil3 Just to be PC, don't assume the OP is a man...it's a safe bet in this field unfortunately, but not always the case
Sorry, she - didn't even look at who posted the question.
1

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

Comments

1

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.)

Comments

0

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; } } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.