我正在通過教程來構建PHP中的模擬Flickr網站。我開始在所有用戶CRUD中添加點。用戶類已經被定義並且具有正在使用和正在使用的方法。但是一旦我添加了用於創建,更新,刪除和保存的方法,就說它找不到它們中的任何一個......我嘗試修改可以工作的方法,但它們不會改變。所以對我來說,似乎有一個用戶類的緩存版本,因爲user.php文件中的更改無法識別。我用CTRL + F5刷新緩存,但是這並不能改變什麼......PHP調用未定義的方法,但方法已定義...(不同於其他兩個類似的問題)
這是我的目錄設置
wamp
www
photo_gallery
includes
config.php
constants.php
database.php
database_object.php
functions.php
initialize.php
session.php
user.php
public
admin
index,php
login.php
logout.php
logfile.php
test.php
images
javascript
layouts
stylesheets
index.php
這是我initialize.php文件,設置了目錄路徑和負載的所有文件所需
<?php
// Define the core paths
// Define them as absolute paths to make sure that require_once works as expected
//DIRECTORY_SEPARATOR is a php pre-defined constant
// (\ for Windows,/for Unix)
defined('DS') ? null : define('DS', DIRECTORY_SEPARATOR);
// checks if lib path exists if not defines the site root with the directory specified below
// ***** needs to be updated when switched directories, servers or computers
defined('SITE_ROOT') ? null :
define('SITE_ROOT', DS.'Users'.DS.'Alan D'.DS.'Desktop'.DS.'wamp'.DS.'www'.DS.'photo_gallery');
// Checks if lib path has been defined, if not it defines it using site path above
defined('LIB_PATH') ? null : define('LIB_PATH', SITE_ROOT.DS.'includes');
// first load config
require_once(LIB_PATH.DS.'config.php');
// load basic functions that are available for all other files
require_once(LIB_PATH.DS.'functions.php');
// load core object classes for application
require_once(LIB_PATH.DS.'session.php');
require_once(LIB_PATH.DS.'database.php');
require_once(LIB_PATH.DS.'database_object.php');
// load database-related object classes
require_once(LIB_PATH.DS.'user.php');
?>
這是用戶級的,它是簡單那麼這時候我的測試,但我繼續通過步驟,使方法能夠跟隨被投進我的其他類當我創造它們時。
<?php
require_once('database.php');
class User extends DatabaseObject{
protected static $db_fields = array('id', 'user_name', 'password',
'first_name', 'last_name');
protected static $table_name="users";
public $id;
public $user_name;
public $password;
public $first_name;
public $last_name;
public function full_name(){
if(isset($this->first_name) && isset($this->last_name)){
return $this->first_name . " " . $this->last_name;
} else {
return "";
}
}
// authenticates user by checking if there is a row with the
// input user name and password, then returns the object if found
// if not it returns false
public static function authenticate($user_name="", $password=""){
global $database;
$user_name = $database->escape_values($user_name);
$password = $database->escape_values($password);
$sql = "SELECT * FROM users ";
$sql .= "WHERE user_name = '{$user_name}' ";
$sql .= "AND password = '{$password}' ";
$sql .= "LIMIT 1";
$result_array = self::find_by_sql($sql);
return !empty($result_array) ? array_shift($result_array) : false;
}
// checks to see if the given attribute exsits for the current object
private function has_attribute($attribute){
// associative array with all attributes key and value pairs
$object_vars = $this->attributes();
// just check if the key exists and return true or false
return array_key_exists($attribute, $object_vars);
}
// returns a key value hash of the objects variables and values
protected function attributes(){
// get_object_vars returns an associative array with all attributes
// (incl private) as the keys and their current values as value
// return get_object_vars($this);
// this goes through the db fields and populates the hash
$attributes = array();
foreach(self::$db_fields as $field){
if(property_exists($this, $field)){
$attributes[$field] = $this->$field;
}
}
return $attributes;
}
// returns a sanitized (sql escaped) array of all the attributes
protected function sanitized_attributes(){
global $database;
$clean_attributes = array();
// sanitize the values before submitting
// Note: does not alter the actual value of each attribute
foreach($this->attributes() as $key => $value){
$clean_attributes[$key] = $database->escape_value($value);
}
return $clean_attributes;
}
// this function check to see if the record is already there
// and determines whether to create or update.
public function save(){
return isset($this->id) ? $this->update() : $this->create();
}
public function create(){
global $database;
$attributes = $this->sanitized_attributes();
$sql = "INSERT INTO ".self::$table_name." (";
$sql .= join(", ", array_keys($attributes));
$sql .= ") VALUES ('";
$sql .= join("', '", array_values($attributes));
$sql .= "')";
if($database->query($sql)){
$this->id = $database->insert_id();
return true;
} else {
return false;
}
}
public function update(){
global $database;
$attributes = $this->sanitized_attributes();
// set up the key, value string needed for the update statement
foreach ($attributes as $key => $value){
$attribute_pairs[] = "{$key}='{$value}'";
}
$sql = "UPDATE ".self::$table_name." SET ";
$sql .= join(", ", $attribute_pairs);
$sql .= " WHERE id='". $database->escape_value($this->id);
$database->query($sql);
return($database->affected_rows() == 1) ? true : false;
}
public function delete(){
global $database;
$sql = "DELETE FROM ".self::$table_name." ";
$sql .= "WHERE id=". $database->escape_value($this->id);
$sql .= " LIMIT 1";
$database->query($sql);
return($database->affected_rows() == 1) ? true : false;
}
}
?>
在管理/ test.php的文件,它設置了這樣
<?php
require_once('../../includes/initialize.php');
if (!$session->is_logged_in()) { redirect_to("login.php"); }
?>
<?php include_layout_template('admin_header.php'); ?>
<?php
$user = new User();
$user->user_name = "jDoe";
$user->password = "pass";
$user->first_name = "Jamie";
$user->last_name = "Doe";
echo $user->full_name();
$user->save();
//$user = User::find_by_id(1);
//$user->password = "pass1";
//$user->update();
?>
<?php include_layout_template('admin_footer.php'); ?>
當我運行該頁面我得到的管理員頭部佈局加載,全名印在屏幕上和它下面說:
致命錯誤:調用未定義的方法,用戶::保存()在C:\ WAMP \ WWW \ photo_gallery \ PUBLIC \ ADMIN \ test.php的上線17
同樣是發生與創建一個nd更新...但這些函數確實存在於user.php文件中,我甚至將所有方法都清空了,以確保它們內部的PHP代碼沒有錯誤。
然後我試圖從公用文件夾的測試,通過公共/ index.php文件
<?php
require_once('../includes/initialize.php');
if (!$session->is_logged_in()) { redirect_to("login.php"); }
?>
<?php include_layout_template('admin_header.php'); ?>
<?php
$user = new User();
$user->user_name = "jDoe";
$user->password = "pass";
$user->first_name = "Jamie";
$user->last_name = "Doe";
$user->full_name();
$user->create();
?>
<?php include_layout_template('admin_footer.php'); ?>
但這並不甚至加載管理員頭部佈局......,只是給了我同樣的錯誤作爲其他測試文件沒有。我相當積極,我正在根據目錄設置導航到正確的路徑。
對於我的生活,我不明白爲什麼/如何將用戶對象添加到user.php中的用戶類時未更新。或者我可以如何使用我以前在用戶中定義的其他方法,但是當我修改它們時,它們不會改變...... php是否執行任何我不知道的對象的緩存?我甚至把user.php從包含目錄中取出......並且所有東西仍然如上所述運行...
任何洞察將不勝感激。我一直在尋找一段時間,試圖找到有類似問題的人,但我找不到一個人。謝謝閱讀。
你或許應該表現出你的'User'類。 –
我剛剛添加了它,之前沒有發佈,因爲我認爲它與目錄設置或緩存有關。因爲我從includes目錄中刪除了user.php文件,並且上面的所有內容仍然工作... –