Skip to content

Instantly share code, notes, and snippets.

@baileylo
Created April 11, 2012 20:07
Show Gist options
  • Save baileylo/2362087 to your computer and use it in GitHub Desktop.
Save baileylo/2362087 to your computer and use it in GitHub Desktop.
Example why you should use type hinting in php where applicable.
<?php
class User {
}
function setUserName(User $user, $new_username) {
$user->Profile->setUserName($new_username);
}
function setUsername1($user, $new_username) {
$user->Profile->setUserName($new_username);
}
// Catchable fatal error: Argument 1 passed to setUserName() must be an instance
// of User, array given, called in FILE on line N and defined in FILE on line N
setUserName(array(), 'Logan');
//Trying to get property of non-object in FILE on line N
//Fatal error: Call to a member function setUserName() on a non-object in FILE on line N
setUserName1(array(), 'Logan');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment