Is it considered bad practice to pair function names with classes?
For example in kohana I could do the following.
function Model($a,$b){
return new Model($a,$b);
}
Then I could drop all references to new, wouldn’t have to make a $temp variable, and no factory clutter.
$temp = new Model('book');
$book=$temp->where('title','=','name')->find();
Or
$book = Model::factory('book')->where('title','=','name');
To
$book = Model('book')->where('title','=','name');
I understand global pollution is bad and it is slightly obscure to read at first, but it has it’s benefits.
I mean it would be even more confusing if someone else defined a function that had no relation to the class, so why not use the function?
For example:
$book = new Model('book');
// looks so close to the above, it's scary
$book = Model('book');
Other Pros or Cons?
I do that occasionally. It sometimes can make code a bit more readable.
It’s called factory procedures. But it makes most sense if they are less shallow. For example if depending on parameters such a method might return different objects (alternative placeholder/stub objects):
But of course you can just use that for nicer looks. It’s basically a cleaner approach than the commonplace static factory methods. But I would reserve such wrapper functions for objects which you really use a lot. It makes no sense to fill up the function scope with a wrapper call for each existing class.
And the common criticism here is: inexperienced developers can easily get confused by the object instantiation without
new. (Not sure if that’s true. But that’s often said in this context.)