[PHP] Core function names as class methods

Soldato
Joined
18 Oct 2002
Posts
5,464
Location
London Town
Just a quick query regarding any possible implications of naming a class method the same as a PHP function (PHP5), other than potentially confusing client code.

e.g. Logger::log() named the same as PHP's log().

As far as I can see there aren't any problems with something like that, but was after some confirmation.
 
Caporegime
Joined
18 Oct 2002
Posts
29,491
Location
Back in East London
Java has shown that sharing names amongst objects is not a problem, so I can't see this being a problem, except for beginners who don't realise $obj->func()/Class::func() is different to just func()

All comes down to the abilities and knowledge of the individual, which all syntaxes suffer from.

However, if it can be avoided easily, I guess it should be.
 
Soldato
OP
Joined
18 Oct 2002
Posts
5,464
Location
London Town
Thanks for the reply. The code is only going to be interacted with by my fellow developers, so syntax won't be a problem (I hope!). I guess context is important too - a method called log() in a class with mathematical functionality would be more confusing than one dealing with logging of messages.

I agree it's better to avoid it if possible, just sometimes it's nicer to use a 'natural' function name than an alternative one dug out of the thesaurus.
 
Caporegime
Joined
18 Oct 2002
Posts
29,491
Location
Back in East London
Indeed :)

Going back onto Java again, it's only functions that have similar actions that share the names.. such as toString() etc. they all acheive the same end result. So on this basis, it might better to ward off temptations to name a method that shares a name with a native function that achieves different results. Using the Logger::log() example, it might actually be a better idea to expand on the functions results a bit in the name, changing it to something like Logger::writeLog() or such, so that anyone who is used to using log() will not look at the class methods and think "what the.. why would a logging class need to run a logarithm?" although the thought would quickly pass in this case, in other cases it may be more problematic. :)
 
Soldato
OP
Joined
18 Oct 2002
Posts
5,464
Location
London Town
Definitely, that makes a lot of sense. I hadn't really considered it that way.
The doSomething() style of function description is the convention I normally follow, but I was tempted by the snappiness of log() in this case. A name like writeLog() it will be :).
 
Soldato
Joined
26 Dec 2003
Posts
16,522
Location
London
I almost always stick to VerbNoun (GetFoo, SetFoo, RenderFoo etc) method naming unless it's really inappropriate. Makes things much easier.

Edit: also, PHP's core nomenclature is absolutely hideous.
 
Last edited:
Back
Top Bottom