Page 1 of 1

Class Not Found Error

Posted: Mon Jan 27, 2014 8:40 am
by rugved
Hello!

Okay I don't know whether this is the right section of the forum where I have posted this question or not. If the latter is the case, I would be happy if the moderators could move it in the appropriate section. :D

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 located in the website root named, example.php. This file contains code to execute a function called, clickFunction() in another file called, Awards.php which is located in the /branding/dataaccesslayer/community/ path of the website folder. This method returns a JSON array to the awards.js page which is displayed in the awards.html page.

The source code of my files is given as follows:

awards.html

Code: Select all

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

Code: Select all

function onrequest() {
    $("#divAddAward").load('example.php');
     $.post(
            'example.php'
            ).success(function(resp) {
        json = $.parseJSON(resp);
        alert(json);
    });
}
example.php

Code: Select all

<?php

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

$class = new Award(); //Error here!
$method =  $class->clickFunction();
echo json_encode($method);
Awards.php

Code: Select all

<?php
class Award extends BaseDocument
 {
    public function __construct() 
    {
        
    }

    public function clickFunction()
    {
       $array = array(
            'Hello' => 'World!'
        );
        return $serverarray;
    }
}
The problem here is that the program is throwing me an error in example.php on line number 7 (marked in the code above) which is, Class 'Awards' not found, despite adding a for loop to locate all the files in the folder.

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

Re: Class Not Found Error

Posted: Mon Jan 27, 2014 9:02 am
by Celauran
Have you walked through the code in example.php? Checked what glob() is returning? Considered using an autoloader?

Re: Class Not Found Error

Posted: Mon Jan 27, 2014 9:07 am
by rugved
Thank you very much for replying.

Can you please explain me what exactly is autoloader?

Sorry for sounding dumb but I am still a rookie in PHP.

Re: Class Not Found Error

Posted: Mon Jan 27, 2014 9:13 am
by Celauran
An autoloader tells PHP where to look for classes it can't find. Take a look at spl_autoload_register.

Consider the following

Code: Select all

class AutoLoader {
	protected static $extension = '.php';

	public static function defaultAutoloader($class) {
		$base_path = __DIR__ . '/vendor/';
		$class     = str_replace('\\', '/', $class);
		$file      = $base_path . $class . self::$extension;

		if (is_readable($file)) {
			require_once $file;
		}
	}
}
spl_autoload_register(array('AutoLoader', 'defaultAutoloader'));
By registering this autoloader, any time PHP can't find a class you're trying to instantiate, it will look in the /vendor/ directory, convert the namespace into directory names, and try to load the file as needed.

Code: Select all

$baz = new \Foo\Bar\Baz();
PHP will now check for the existence of /vendor/Foo/Bar/Baz.php, include that, and create the requested object.

Re: Class Not Found Error

Posted: Mon Jan 27, 2014 9:30 am
by rugved
So can you please provide me the actual line of code which will help me resolve my question?

Re: Class Not Found Error

Posted: Mon Jan 27, 2014 9:40 am
by Celauran
rugved wrote:So can you please provide me the actual line of code which will help me resolve my question?
Can you please answer my questions?
Celauran wrote:Have you walked through the code in example.php? Checked what glob() is returning?

Re: Class Not Found Error

Posted: Tue Jan 28, 2014 1:58 am
by rugved
Celauran wrote:
rugved wrote:So can you please provide me the actual line of code which will help me resolve my question?
Can you please answer my questions?
Celauran wrote:Have you walked through the code in example.php? Checked what glob() is returning?
What do you mean by, 'walked through the code'? Could you please elaborate? If you mean to say debugging, then I have tried debugging my project in NetBeans but the call stack does not pause at the location where I have inserted a breakpoint. This is another problem I need to solve but will do it after this one gets resolved.

Re: Class Not Found Error

Posted: Tue Jan 28, 2014 5:38 am
by Celauran
Forget about the AJAX call. Access the page directly, echo out the results of your glob(). Is it returning the necessary file?

Re: Class Not Found Error

Posted: Wed Jan 29, 2014 12:49 am
by rugved
Okay I resolved the error myself. I hadn't placed the Awards.php file in the path specified in the foreach loop. The correct code snippet to include all the files is as follows:

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

Sorry for the silly mistake and thank you for your help. :)