Class Not Found Error

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
rugved
Forum Newbie
Posts: 23
Joined: Tue Dec 03, 2013 12:33 am
Location: Cyber World

Class Not Found Error

Post 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.
Last edited by rugved on Mon Jan 27, 2014 9:12 am, edited 2 times in total.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Class Not Found Error

Post by Celauran »

Have you walked through the code in example.php? Checked what glob() is returning? Considered using an autoloader?
User avatar
rugved
Forum Newbie
Posts: 23
Joined: Tue Dec 03, 2013 12:33 am
Location: Cyber World

Re: Class Not Found Error

Post 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.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Class Not Found Error

Post 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.
User avatar
rugved
Forum Newbie
Posts: 23
Joined: Tue Dec 03, 2013 12:33 am
Location: Cyber World

Re: Class Not Found Error

Post by rugved »

So can you please provide me the actual line of code which will help me resolve my question?
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Class Not Found Error

Post 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?
User avatar
rugved
Forum Newbie
Posts: 23
Joined: Tue Dec 03, 2013 12:33 am
Location: Cyber World

Re: Class Not Found Error

Post 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.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Class Not Found Error

Post by Celauran »

Forget about the AJAX call. Access the page directly, echo out the results of your glob(). Is it returning the necessary file?
User avatar
rugved
Forum Newbie
Posts: 23
Joined: Tue Dec 03, 2013 12:33 am
Location: Cyber World

Re: Class Not Found Error

Post 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. :)
Post Reply