Skip to content

Instantly share code, notes, and snippets.

@abcarroll
Created December 20, 2015 01:00
Show Gist options
  • Save abcarroll/cf605883c08507426850 to your computer and use it in GitHub Desktop.
Save abcarroll/cf605883c08507426850 to your computer and use it in GitHub Desktop.
<?php
// Just showing you: say nonexistent had defined a variable called $foo that is a boolean, we can hint that too
include 'nonexistent.php';
/** @var bool $foo */
if($foo) { // this won't say undefined variable in the editor!
echo "foo was true in nonexistent.php";
}
class x {
/**
* @return array
*/
public function getContainer() {
return [];
}
/**
* Returns true if a user exists with the given username or email, false otherwise
*
* @param string $username The username
* @param string $email The email address
*
* @return boolean If a user exists with given username or email address.
*/
public function exists($username, $email){
/**
* @var PDO $db
*/
$db = $this->getContainer()['db'];
$stmt = $db->prepare("SELECT COUNT(*) AS count FROM userprofile WHERE username = :username OR email = :email");
$stmt->execute(array(':name'=>$username, ':email'=>$email));
$results = $stmt->fetch(\PDO::FETCH_ASSOC);
// Let's pretend that $results was an array of Iterators
// as in the built in "iterator" class: http://us1.php.net/manual/en/class.iterator.php
// you can even hint at $x in the loop's scope
foreach($results as $x) {
/** @var Iterator $x */
$x->
}
#return (int) $results['count'] > 0 ? true : false;
echo $username, $email, "\n";
return true;
}
}
$x = new x();
$x->exists("username", "email");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment