PHP调用未定义的方法,但定义了方法...(与其他2个类似的问题不同)

时间:2012-09-18 20:13:43

标签: php caching directory fatal-error

我正在通过教程在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()

创建和更新也发生了同样的事情......但是这些函数确实存在于user.php文件中,我甚至清空了方法中的所有内容,以确保它们内部的php代码中没有错误。< / p>

然后我通过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 ...并且所有内容仍然如上所述运行...

非常感谢任何见解。我一直在寻找一段时间试图寻找有类似问题的人,但我找不到一个。谢谢你的阅读。

1 个答案:

答案 0 :(得分:1)

在您的initialize.php中,您在SITE_ROOT定义中有一个额外的目录,与您在目录结构中显示的目录相比。你在photo_gallery目录中是否还有这个代码的另一个副本,这实际上是包含来自的文件?

这是您的所有包含指向的地方:

/Users/Alan D/Desktop/wamp/www/photo_gallery/includes