I want a function in my class to perform a simple task, such as:
function hello($name)
{
return 'hello '.$name;
}
i.e. Not necessarily static (though I suppose it might be), but not related to the object (no reference to $this necessary).
Do I use a static function? ie.
static function hello($name){return 'hello '.$name;}
and call it using $string = ClassName::hello('Alex');
or is there a better way?
Thanks!
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.