我正在学习一个用 PHP 构建模拟 Flickr 站点的教程。我正在添加所有用户 CRUD。用户类已经被定义并且具有正在使用和工作的方法。但是,一旦我添加了创建、更新、删除和保存的方法,它就会说找不到它们中的任何一个……我尝试修改有效的方法,但它们并没有改变。所以对我来说,似乎有一个用户类的缓存版本,因为 user.php 文件中的更改没有被识别。我已经使用 ctrl+F5 来刷新缓存,但这并没有改变任何东西......
这是我设置的目录
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;
}
}
?>
在 admin/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'); ?>
当我运行该页面时,我会加载管理标题布局,全名打印在屏幕上,并在其正下方显示:
致命错误:在第 17 行调用 C:\wamp\www\photo_gallery\public\admin\test.php 中未定义的方法 User::save()
create 和 update 也是如此……但是这些函数确实存在于 user.php 文件中,我什至清空了方法中的所有内容,以确保其中的 php 代码没有错误。
然后我通过 public/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 从包含目录中拉出……一切仍按上述说明运行……
任何见解将不胜感激。我一直在寻找一段时间,现在试图找到有类似问题的人,但我找不到。谢谢阅读。