(new Query())->from('table')->getOne() A setter should always return void. I would prefer for functions wtihout set prefix, that they always return the original objet itself.
Except for get*, has*, is*, to* (e.g. toString() )
Google a bit about this: found an interesting opinion, I am not sold on it, have to check it again after having slept over it.
edit: made the example code show how I wished it to be, instead of how it looks like when not.
My humble opinion:
I like the point 2, in the way code will be much more readable, and stricter means less code error possibilities, but I am not really sure.
This should be used for strict setters that does nothing but setting/getting
These should reflect exactly what they say. For example isRunning() and shouldRun() should just do that, checking whether it is already running or we are allowed even to run at that point.
Same as above, me thinks
Am fine with name that reflects what it is doing
On Route is should be getHost() really host sounds like a command to host than a getter. So is to which is I don’t know what it does from reading it. Have to consult docs (which is bad. Mehods should try to be self docmenting)
noun is context
if noun is hide, mean use object it self as context
i.e
Log::write() mean Log::writeLog()
use adjective is for more expline method, i.e Log::writeAllLogs()
use single adjective as method name is multiple interpretations
ie. $query->all() , What does it mean?
on current DB, this method fetch all record
it’s more better use $query->fetchAll()
or if all() method not return record but only condition
the code become $query->all()->fetch().
I think for the PHP world immutable setters are still a pretty new practice that it really may be worth to enforce a with prefix - even if the interface is not so nice anymore.
I see tons of mistakes like this:
// Stupid example, I know, but you get the idea
$route = Route::get('/complex', [SomeController::class, 'complex']);
$names = require 'mynames.php';
$route->name($names['complex']);
return [
$route,
];
In fact the concept of “immutable setters” must be made very very clear and all places where it’s used must have big warning signs or something. I would even question, why they are used in the Route class at all.
Departure from the naming convention is a source of potential errors. If we accept that an immutable property is prefixed with with, then we must strictly follow this.
If we want to move away from the convention, then perhaps this property should not be made immutable? In fact, I see it as a trend towards everything new. At first glance this seems cool, but in the future it is possible that it will look like a fashion of the past (I mean immutable properties).
Without going into context, you might think that MatchingResult::methods() is a setter.