As Flambino said, why are you using static properties and methods? Static really contradicts the whole purpose of OOP and so it should really only be used in special cases. In fact, those special cases aren't likely to present themselves until you are much more familiar with the concept of OOP, therefore, if you ever find yourself using static elements you can stop yourself immediately and ask, "Why?". More than likely there is a better way to do what you are attempting without them.
That being said, the way you are currently listing your properties is fine, but just in case you didn't already know, you only have to declare public
once. You can separate each additional property with a comma. And since PHP doesn't care about whitespace, you can make this more legible by adding a newline after each property. This also works for properties that have default values, as demonstrated below. As Corbin pointed out in the comments, this is a preference, you can do either one.
public $app, $user = 'default user', //etc... $text ;
PHP's empty()
function is really only useful on arrays, and objects. When using strings it is better to use is_null()
or just treat the parameter/variable as a boolean directly. Note: Doing the later will have unexpected results if the parameter's value is ever a false value (0, '', "false", etc...). By the way, PHP inherently requires braces. Otherwise, when you add additional lines to a statement, you wouldn't have to then also add braces. As such, you should really use braces all the time, even on single line statements. People might argue this, but I feel that if PHP does not allow completely braceless code it shouldn't allow it at all, otherwise it is only going to cause issues. Additionally, you can combine those two if statements into one using the OR short circuiting operator. This follows the "Don't Repeat Yourself" (DRY) Principle, which, along with the Single Responsibility Principle, are important concepts when trying to learn OOP.
if( is_null( $title ) || is_null( $text ) ) { return FALSE; } //OR if( ! $title || ! $text ) { return FALSE; }
Speaking of Single Responsibility Principle, you are doing too much in your status method. The method should only do as much as is required to accomplish its purpose. In this case return the status. It can call as many methods as it needs to accomplish this, but it shouldn't be directly responsible for doing anything else. This ensures completely reusable code, which, as mentioned above, is a key concept of OOP. However, in this context, I don't believe the status()
method should really have to call any other methods.
Last, but not least, try to cut back on internal comments. If you follow the Single Responsibility Principle then your code should naturally fall into a "self-documenting" style, and then the only comments you will need are doccomments for recording more abstract functionality.
Hope this helps!
Noti::status(title,text);
and it does a cURL post to a url with that data. It's really not anything major :)\$\endgroup\$abstract
in there too), yet there's also someuser_token
stuff. So is this for a single-user system or are there several users to notify (in which case a non-static class might make more sense)?. I understand how you call this code and what it does, but I still don't know much about the context\$\endgroup\$Noti::status('test', 'testing');
there is not going to be several users, there is a single user and a single app id, and a single url to notify. I could have made this a function, but the way my framework works is that classes are easier to work with as they are autoloaded.\$\endgroup\$