mysql - PHP - Constructing a class with PDO - Warning: Missing argument -
i have following select statement constructing user object:
$stmt = $conn->prepare("select user.user_id, user.user_name user"); $stmt->execute(); $stmt->setfetchmode(pdo::fetch_class| pdo::fetch_props_late,'user');
i have following class in separate file, included - require "user.php":
class user { private $user_id; private $user_name; public function __construct($user_id, $user_name) { $this->user_id = $user_id; $this->user_name =$user_name; }
i following warnings , notices -
warning: missing argument 1 user::__construct() notice: undefined variable: user_id
it works fine turned on warnings etc tidying code.
i have looked @ other questions on here solutions don't seem , thought fresh pair of eyes help. thought had should have been passing array in setfetchmode() documentation states still seems cause issues.
what correct way create class in pdo?
if use arguments user constructor, need use third parameter in setfetchmode
public bool pdostatement::setfetchmode ( int $pdo::fetch_class , string $classname , array $ctorargs )
in case
$stmt->setfetchmode(pdo::fetch_class| pdo::fetch_props_late,'user', array('user_id','user_name'));
or provide default values constructor.
see the reference.
Comments
Post a Comment