I'm just getting into OOP with php and discovered almost by accident that if I put the object type before the name in the function definition that the IDE I'm using can resolve member functions and data members for the object.
For example:
public function foo(car $car) { }
As opposed to:
public function foo($car) { }
Is the first method legitimate and preferred?
Thanks for helping a noob :-)
Definitely: Type Hinting. Thats a feature of PHP itself and the IDE just makes you a favour with giving you more useful code completion. Try it out and pass an object of the wrong type. This will help you to find bugs, that result from wrong types passed to methods, much earlier.
class A{}
class B{}
function foo(A $a) { echo "OK";}
foo(new A);
foo(new B);
Also you should have a look at doccomments (sorry, no link). Most IDEs are able to read and use them for additional code completion suggestions too.
/**
* @return MyObject
*/
function bar(){/* ..*/};
bar()->[Strg-Space]
Actually , the the best practice would be to hint the interface, not a specific class name. This goes back to Interface Segregation Principle in object oriented application design.
This means, that, when you are injecting an object, you should expect that object to have a specific method footprint, which you can then call on it. Thats how polymorphism works.