mysql_fetch_object doesn't work native as the type of the object fetched isn't the right one, but with a small typecast it works flawlessly.
PHP Code:
<?php
function ClassTypeCast ($obj, $class) {
if (class_exists($class)) {
$ser = '/^O:[0-9]+:\\"[^"]+\\":/i';
$rep = 'O:' . strlen($class) . ':"' . $class . '\\":';
$obj = serialize($obj);
$obj = preg_replace($ser, $rep, $obj);
$obj = unserialize($obj);
return $obj;
}
}
class Player {
private $user;
private $player;
public function __construct ($user) {
$query = mysql_query("SELECT * FROM players WHERE name='$user' LIMIT 1");
$this->player = ClassTypeCast(mysql_fetch_object($query), __CLASS__);
#$this->setName($this->player->name); # This makes no sense. Why assign something already assigned?
#echo $this->player->name;
}
public function setName ($name) {
$this->player->name = $name;
}
public function getName () {
return $this->player->name;
}
}
dbconnect();
$player = new Player('Jonathan');
$player->setName('Bob');
echo $player->getName(); # You need ()
Bookmarks