Memor.の 2014-01-24 06:36 采纳率: 100%
浏览 26

PHP中找不到类错误

I am a rookie PHP and MongoDB developer.

I have created a PHP web project with an HTML page that contains an 'Add' button. The name of the page is awards.html. The awards.html file contains its counterpart JavaScript file, awards.js. A code is executed in this js file when the Add button is clicked. This code sends an AJAX call to a PHP class elsewhere in the project named, example.php which contains code to execute a function called, clickFunction() in an Awards.php file, which returns a JSON array to the awards.html page.

The source code of my files is given as follows:

Awards.html

<div class = "divbottom">
    <div id="divAddAward">
        <button class="btn" onclick="onrequest();">Add</button>
    </div>
</div>

awards.js

function onrequest() {
    $("#divAddAward").load('branding/dataaccess/example.php'); //The full path of the example.php file in the web root
    alert('Test');
    $.post(
            'branding/dataaccess/example.php'
            ).success(function(resp) {
        json = $.parseJSON(resp);
        alert(json);
    });
}

example.php

<?php

foreach (glob("App/branding/data/*.php") as $filename) {
    include $filename;
}

$class = new Awards();
$method =  $class->clickFunction();
echo json_encode($method);

Awards.php

<?php

class Awards extends Mongo_db {

    //put your code here

    public function __construct() {
        parent::__construct();
    }

    public function clickFunction() {
        $array = array(
            'status' => '1'
        );
        return $array;
    }

}

The problem here is that the program is throwing me an error in example.php, Class 'Awards' not found, despite adding the for loop to include all the files. The Awards.php file is located in App/branding/data/ path and example.php is located in App/branding/dataaccess/ path.

Can anyone please tell me where exactly am I going wrong? Replies at the earliest will be highly appreciated. Thank you in advance.

  • 写回答

3条回答 默认 最新

  • 叼花硬汉 2014-01-24 06:44
    关注

    Update example.php to

    <?php
    
    foreach (glob('../data/*.{php}', GLOB_BRACE) as $filename) {
        //Echo out each included file to make sure it's included
        echo $filename . '<br />'; 
        include $filename;
    }
    
    $class = new Awards();
    $method =  $class->clickFunction();
    echo json_encode($method);
    
    评论

报告相同问题?

悬赏问题

  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题