【发布时间】:2014-10-31 21:54:30
【问题描述】:
这是Reflection::export(new ReflectionClass($this));的回复
Class [ class Users extends Crud ] {
@@ C:\xampp\htdocs\classes\Users.php 5-76
- Constants [0] {
}
- Static properties [0] {
}
- Static methods [0] {
}
- Properties [7] {
Property [ protected $table ]
Property [ private $username ]
Property [ private $email ]
Property [ private $password ]
Property [ private $passwordRepeat ]
Property [ private $nickname ]
Property [ private $permission ]
}
- Methods [20] {
Method [ public method setUsername ] {
@@ C:\xampp\htdocs\classes\Users.php 15 - 17
- Parameters [1] {
Parameter #0 [ $username ]
}
} Method [ public method setEmail ] {
@@ C:\xampp\htdocs\classes\Users.php 19 - 21
- Parameters [1] {
Parameter #0 [ $email ]
}
} Method [ public method setPassword ] {
@@ C:\xampp\htdocs\classes\Users.php 23 - 28
- Parameters [2] {
Parameter #0 [ $password ]
Parameter #1 [ $passwordRepeat ]
}
} Method [ public method setNickname ] {
@@ C:\xampp\htdocs\classes\Users.php 30 - 32
- Parameters [1] {
Parameter #0 [ $nickname ]
}
} Method [ public method setPermission ] {
@@ C:\xampp\htdocs\classes\Users.php 34 - 36
- Parameters [1] {
Parameter #0 [ $permission ]
}
} Method [ public method checkUsername ] {
@@ C:\xampp\htdocs\classes\Users.php 38 - 49
- Parameters [2] {
Parameter #0 [ $username ]
Parameter #1 [ $email ]
}
} Method [ public method insert ] {
@@ C:\xampp\htdocs\classes\Users.php 51 - 64
} Method [ public method update ] {
@@ C:\xampp\htdocs\classes\Users.php 66 - 75 - Parameters [1] {
Parameter #0 [ $id ]
}
} Method [ public method find ] {
@@ C:\xampp\htdocs\classes\Crud.php 12 - 18
- Parameters [1] {
Parameter #0 [ $id ]
}
} Method [ public method findAll ] {
@@ C:\xampp\htdocs\classes\Crud.php 20 - 26
} Method [ public method delete ] {
@@ C:\xampp\htdocs\classes\Crud.php 28 - 33
- Parameters [1] {
Parameter #0 [ $id ]
}
} /** * Create a new DB object for the specified database type but don't * connect to the database * * @param string $type the database type (eg "mysql") * @param array $options an associative array of option names and values * * @return object a new DB object. A DB_Error object on failure. * * @see DB_common::setOption() */
Method [ public method &factory ] {
@@ C:\xampp\php\pear\DB.php 447 - 480
- Parameters [2] {
Parameter #0 [ $type ]
Parameter #1 [ $options = false ]
}
} /** * Create a new DB object including a connection to the specified database * * Example 1. * * require_once 'DB.php'; * * $dsn = 'pgsql://user:password@host/database'; * $options = array( * 'debug' => 2, * 'portability' => DB_PORTABILITY_ALL, * ); * * $db =& DB::connect($dsn, $options); * if (PEAR::isError($db)) { * die($db->getMessage()); * } * * * @param mixed $dsn the string "data source name" or array in the * format returned by DB::parseDSN() * @param array $options an associative array of option names and values * * @return object a new DB object. A DB_Error object on failure. * * @uses DB_dbase::connect(), DB_fbsql::connect(), DB_ibase::connect(), * DB_ifx::connect(), DB_msql::connect(), DB_mssql::connect(), * DB_mysql::connect(), DB_mysqli::connect(), DB_oci8::connect(), * DB_odbc::connect(), DB_pgsql::connect(), DB_sqlite::connect(), * DB_sybase::connect() * * @uses DB::parseDSN(), DB_common::setOption(), PEAR::isError() */
Method [ public method &connect ] {
@@ C:\xampp\php\pear\DB.php 518 - 567
- Parameters [2] {
Parameter #0 [ $dsn ]
Parameter #1 [ $options = Array ]
}
} /** * Return the DB API version * * @return string the DB API version number */
Method [ public method apiVersion ] {
@@ C:\xampp\php\pear\DB.php 577 - 580
} /** * Determines if a variable is a DB_Error object * * @param mixed $value the variable to check * * @return bool whether $value is DB_Error object */
Method [ public method isError ] {
@@ C:\xampp\php\pear\DB.php 592 - 595
- Parameters [1] {
Parameter #0 [ $value ]
}
} /** * Determines if a value is a DB_ object * * @param mixed $value the value to test * * @return bool whether $value is a DB_ object */
Method [ public method isConnection ] {
@@ C:\xampp\php\pear\DB.php 607 - 612
- Parameters [1] {
Parameter #0 [ $value ]
}
} /** * Tell whether a query is a data manipulation or data definition query * * Examples of data manipulation queries are INSERT, UPDATE and DELETE. * Examples of data definition queries are CREATE, DROP, ALTER, GRANT, * REVOKE. * * @param string $query the query * * @return boolean whether $query is a data manipulation query */
Method [ public method isManip ] {
@@ C:\xampp\php\pear\DB.php 628 - 639
- Parameters [1] {
Parameter #0 [ $query ]
}
} /** * Return a textual error message for a DB error code * * @param integer $value the DB error code * * @return string the error message or false if the error code was * not recognized */
Method [ public method errorMessage ] {
@@ C:\xampp\php\pear\DB.php 652 - 694
- Parameters [1] {
Parameter #0 [ $value ]
}
} /** * Parse a data source name * * Additional keys can be added by appending a URI query string to the * end of the DSN. * * The format of the supplied DSN is in its fullest form: * * phptype(dbsyntax)://username:password@protocol+hostspec/database?option=8&another=true * * * Most variations are allowed: * * phptype://username:password@protocol+hostspec:110//usr/db_file.db?mode=0644 * phptype://username:password@hostspec/database_name * phptype://username:password@hostspec * phptype://username@hostspec * phptype://hostspec/database * phptype://hostspec * phptype(dbsyntax) * phptype * * * @param string $dsn Data Source Name to be parsed * * @return array an associative array with the following keys: * + phptype: Database backend used in PHP (mysql, odbc etc.) * + dbsyntax: Database used with regards to SQL syntax etc. * + protocol: Communication protocol to use (tcp, unix etc.) * + hostspec: Host specification (hostname[:port]) * + database: Database to use on the DBMS server * + username: User name for login * + password: Password for login */ Method [ public method parseDSN ] { @@ C:\xampp\php\pear\DB.php 733 - 850 - Parameters [1] { Parameter #0 [ $dsn ] } } /** * Returns the given DSN in a string format suitable for output. * * @param array|string the DSN to parse and format * @param boolean true to hide the password, false to include it * @return string */
Method [ public method getDSNString ] {
@@ C:\xampp\php\pear\DB.php 862 - 926
- Parameters [2] {
Parameter #0 [ $dsn ]
Parameter #1 [ $hidePassword ]
}}}}
用Reflection::export(new ReflectionClass('DB'));
我收到以下回复:
Class [ class DB ] {
@@ C:\xampp\htdocs\classes\DB.php 5-30
- Constants [0] {
}
- Static properties [1] {
Property [ private static $instance ]
}
- Static methods [2] {
Method [ static public method getInstance ] {
@@ C:\xampp\htdocs\classes\DB.php 9 - 24
}
Method [ static public method prepare ] {
@@ C:\xampp\htdocs\classes\DB.php 26 - 28 - Parameters [1] {
Parameter #0 [ $sql ]
}
}
}
- Properties [0] {
}
- Methods [0] {
} }
所以,我是 PHP 新手,我正在尝试学习一点 PDO。 我有这段代码,它一直显示这条消息:
致命错误:在第 22 行调用 C:\xampp\htdocs\classes\Crud.php 中未定义的方法 DB::prepare()
这是我的 DB.php:
<?php
require_once 'cfg.php';
class DB{
private static $instance;
public static function getInstance(){
if(!isset(self::$instance)){
try {
self::$instance = new PDO('mysql:host=' . DB_HOST . ';dbname=' . DB_NAME, DB_USER, DB_PASS);
self::$instance->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
self::$instance->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ);
} catch (PDOException $e) {
echo $e->getMessage();
}
}
return self::$instance;
}
public static function prepare($sql){
return self::getInstance()->prepare($sql);
}
}
?>
Crud.php:
<?php
require_once 'DB.php';
abstract class Crud extends DB {
protected $table;
abstract public function insert();
abstract public function update($id);
public function find($id) {
$sql = "SELECT * FROM $this->table WHERE ID = :id";
$qry = DB::prepare($sql);
$qry->bindParam(':id', $id, PDO::PARAM_INT);
$qry->execute();
return $qry->fetch();
}
public function findAll() {
$sql = "SELECT * FROM $this->table";
$qry = DB::prepare($sql);
$qry->execute();
return $qry->fecthAll();
}
public function delete($id) {
$sql = "DELETE FROM $this->table WHERE ID = :id";
$qry = DB::prepare($sql);
$qry->bindParam(':id', $id, PDO::PARAM_INT);
return $qry->execute();
}
}
?>
然后,当我尝试从代码中获取任何功能时,我会收到该消息。 例如,如果我尝试这样做:
<?php
$crud = new Users();
$crud->findAll();
?>
Users.php:
<?php
require_once 'Crud.php';
class Users extends Crud {
protected $table = 'bloggy_user';
private $username;
private $email;
private $password;
private $passwordRepeat;
private $nickname;
private $permission;
public function setUsername($username) {
$this->username = $username;
}
public function setEmail($email) {
$this->email = $email;
}
public function setPassword($password, $passwordRepeat) {
if ($password == $passwordRepeat) {
$cryptedPass = password_hash($password, PASSWORD_BCRYPT, ['cost' => 10, 'salt' => '1234567890123456789011']);
$this->password = $cryptedPass;
}
}
public function setNickname($nickname) {
$this->nickname = $nickname;
}
public function setPermission($permission) {
$this->permission = $permission;
}
public function checkUsername($username, $email) {
$sql = "SELECT * FROM $this->table WHERE username = :username OR email = :email";
$qry = DB::prepare($sql);
$qry->bindParam(':username', $username);
$qry->bindParam(':email', $email);
$qry->execute();
if ($qry->fetchColumn() == 0) {
return true;
} else {
return false;
}
}
public function insert() {
$sql = "INSERT INTO $this->table (username, email, password, nickname) VALUES (:username, :email, :password, :nickname)";
$qry = DB::prepare($sql);
$qry->bindParam(':username', $this->username);
$qry->bindParam(':email', $this->email);
$qry->bindParam(':password', $this->password);
$qry->bindParam(':nickname', $this->nickname);
if ($this->checkUsername($this->username, $this->email)) {
return $qry->execute();
} else {
echo "Esse usuário já existe";
return false;
}
}
public function update($id) {
$sql = "UPDATE $this->table SET email = :email, password = :password, nickname = :nickname, permission = :permission WHERE ID = :id";
$qry = DB::prepare($sql);
$qry->bindParam(':email', $this->email);
$qry->bindParam(':password', $this->password);
$qry->bindParam(':nickname', $this->nickname);
$qry->bindParam(':permission', $this->permission);
$qry->bindParam(':id', $id);
return $qry->execute();
}
}
?>
它确实显示了错误消息。 如果我说得够清楚的话,我想知道,但我真的是 PDO 的菜鸟,所以如果有人能帮助我,我将不胜感激。
【问题讨论】:
-
Users类到底在哪里?您遗漏了一些相关数据供我们分析。 -
你为什么要扩展(或使用...)一个单例?
-
抱歉,我将编辑并添加用户类
-
无法复制,见codepad.viper-7.com/rPbe0y;它死于
DB类中的prepare方法。 -
但是为什么它在 prepare 方法中死掉了?