0

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コードにバグがないことを確認しました.

次に、 public/index.php を介して public フォルダーからテストを試みました

<?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を取り出しました...そして、上記で説明したようにすべてがまだ実行されています...

どんな洞察も大歓迎です。私はしばらくの間、同様の問題を抱えている人を見つけようとして探していましたが、見つけることができません。読んでくれてありがとう。

4

1 に答える 1

1

あなたの initialize.php にはSITE_ROOT、ディレクトリ構造に表示されているものと比較して、定義に追加のディレクトリがあるようです。photo_galleryおそらく、これが実際にファイルを含むディレクトリに、このコードの別のコピーがまだありますか?

これは、すべてのインクルードが指している場所です。

/Users/Alan D/Desktop/wamp/www/photo_gallery/includes
于 2012-09-18T21:09:01.937 に答える