Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/259.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP调用未定义的方法,但方法已定义。。。(与其他2个类似问题不同)_Php_Caching_Directory_Fatal Error - Fatal编程技术网

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

PHP调用未定义的方法,但方法已定义。。。(与其他2个类似问题不同),php,caching,directory,fatal-error,Php,Caching,Directory,Fatal Error,我正在学习一个用PHP构建模拟Flickr站点的教程。我已经到了添加所有用户CRUD的地步。用户类已经定义,并且具有正在使用和工作的方法。但是,一旦我添加了创建、更新、删除和保存的方法,它就会说找不到任何方法。。。我尝试过修改有效的方法,但它们没有改变。因此,在我看来,似乎存在user类的缓存版本,因为user.php文件中的更改无法识别。我使用了ctrl+F5来刷新缓存,但这并没有改变任何事情 这是我的目录设置 wamp www photo_gallery includ

我正在学习一个用PHP构建模拟Flickr站点的教程。我已经到了添加所有用户CRUD的地步。用户类已经定义,并且具有正在使用和工作的方法。但是,一旦我添加了创建、更新、删除和保存的方法,它就会说找不到任何方法。。。我尝试过修改有效的方法,但它们没有改变。因此,在我看来,似乎存在user类的缓存版本,因为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;
        }       
    }
?>

当我运行该页面时,我得到了要加载的管理员标题布局,全名打印在屏幕上,并在其正下方显示:

致命错误:在第17行的C:\wamp\www\photo\u gallery\public\admin\test.php中调用未定义的方法User::save()

创建和更新也发生了同样的情况。。。但是这些函数确实存在于user.php文件中,我甚至清空了方法中的所有内容,以确保其中的php代码中没有bug

然后我通过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中向用户类添加内容时,为什么/如何不更新用户对象。或者我如何使用我以前在user中定义的其他方法,但当我修改它们时它们不会改变。。。php是否对我不知道的对象进行缓存?我甚至把user.php从includes目录中拉了出来。。。所有的一切都像上面解释的那样运行


如有任何见解,将不胜感激。我已经搜索了一段时间,现在试图找到有类似问题的人,但我找不到。感谢阅读。

与目录结构中显示的目录相比,在initialize.php中,
站点的根目录定义中似乎有一个额外的目录。您是否还在
photo_gallery
目录中有此代码的另一份副本,而实际上它包含了来自此目录的文件

这是您的所有Include都指向的地方:

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

您可能应该显示您的
User
类。我刚刚添加了它,以前没有发布,因为我认为它与目录设置或缓存有关。由于我从includes目录中删除了user.php文件,并且上面的所有内容都仍然工作正常……我想你是对的:X我想我的桌面上有一份该项目的副本,还有C:\wamp\www\photo\u gallery\public,我不确定这是怎么发生的。。。我不得不重新安装wamp几次,因为apache无法启动我想当我切换工作目录时,我忘记了编辑该文件。。。就像我说过我需要做的那样。。。感谢您查看我这一愚蠢的错误。请注意,如果您处理的代码库将在目录结构中移动,您可能应该使用PHP magic constants
\uuuuuuu FILE\uuuuuuu
\uuuu DIR\uuuu
等自动为您设置站点根目录,与初始化文件目录位置相关的所有链接。非常有效的一点,我主要是按照教程。然后,当我到达终点时,我将修复一系列问题,使路径动态化肯定是其中之一。再次感谢。
/Users/Alan D/Desktop/wamp/www/photo_gallery/includes