PHP Class functions that are neither methods nor static?

Class methods which don't require an instance of object to be called and which should be able to be executed without an instance of object should be declared as static Static methods don't have $this and should be called as ClassName::methodName() Static methods can access static member variables of their class.

Class methods which don't require an instance of object to be called and which should be able to be executed without an instance of object should be declared as static. Static methods don't have $this and should be called as ClassName::methodName(). Static methods can access static member variables of their class.

I thought as much; I was just irritated by calling the static function because it looks strange - still getting used to using Objects with PHP. Thanks! – Alex coady Aug 2 at 10:03 A good practice is to put all your functions into classes or namespaces.

Better than having a ton of global functions. – scaryzet Aug 2 at 10:06.

A static function should be a static function. If it is stateless, than use static. You may also encapsulate group of similar functions in a class *Utils.So these functions will be like helpers class StringUtils{ function splitBy($delimeter,$val){....} } than you call it StringUtils::splitBy(..) meaning if it is not related to the object, seperate it.

You can take with you the utils folder to every project and reuse it on and on and on....

Ah that's a lovely idea, thank you! – Alex coady Aug 2 at 10:04 Any class named *Utils is a code smell. – Gordon Aug 2 at 10:12 @Gordon, y?

Any chance to reuse a function inside an unrelated object? It is bad to UserEntity::SplitString($...) how can a different programmer find that? I think this better StringUtils::SplitString($...) – fatnjazzy Aug 2 at 10:41 Because an object should have methods that operate on it's own internal state.An object that doesn't do that is lacking coherence and that's a sure sign of a misplaced method.

Grouping seemingly related functions into a class scope is basically just abusing the class for a namespace. – Gordon Aug 2 at 10:51 So, how do you reuse a code? – fatnjazzy Aug 2 at 11:03.

If it not belong to it - just move somewhere else ... if belongs then the question is in possibility to call it without creating object.

If there is no reference to $this (or possible future reference to $this) make it static. I say this because sometimes I go with: static function hello( $name ) { return 'hello '. $name; } and after a few months of developing and expanding the program I feel the need to reference $this, like: function hello( $name ) { return $this->helloInLanguage $this->language .' '.

$name; }.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions