I'm relatively new to PHP OOP and cant seem to be able to call functions in a PHP Class via a controller. My current setup is:
Within index.php i include class.Class1.php, make a new Class object and use a couple of it's methods to output some HTML. I want to use js functions in script.js (called by using the onclick attribute of a link) to communicate with functions.controller.php, which in turn will call methods in class.Class1.php and return the data to script.js who will in turn output HTML on index.php.
The problem is that i dont know how to use the methods to get the data of that particular object. When i call the controller:
$.ajax({
type: 'GET',
url: 'functions/functions.controller.php',
data: 'r=' + 'h',
success: function(data){
$('.showing').html(data);
}
});
I get a 500 Internal server error:
[25-Oct-2011 01:24:06] PHP Warning: include(classes/class.Class1.php) [function.include]: failed to open stream: No such file or directory in /Users/Joey/Desktop/root/Test/functions/functions.controller.php on line 7
[25-Oct-2011 01:24:06] PHP Warning: include() [function.include]: Failed opening 'classes/class.Class1.php' for inclusion (include_path='.:/Applications/MAMP/bin/php5.3/lib/php') in /Users/Joey/Desktop/root/Test/functions/functions.controller.php on line 7
[25-Oct-2011 01:24:06] PHP Fatal error: Class 'Class1' not found in /Users/Joey/Desktop/root/Test/functions/functions.controller.php on line 9
This is controller.php:
include('classes/class.Class1.php');
$in = new Class1;
echo $in->getData();
Can anyone please shed some light on this? Also any other PHP OOP MVC with AJAX tips are welcome. If more of my current code is needed, i'll be happy to post.
Looks like you're getting your include paths mixed up.
I'm assuming your file structure is actually
index.php
classes/class.Class1.php
functions/functions.controller.php
script.js
To include class.Class1.php
from functions.controller.php
, you need to traverse up a directory first to get out of the functions
directory, eg
// PHP >= 5.3
require_once __DIR__ . '/../classes/class.Class1.php';
// PHP < 5.3
require_once dirname(__FILE__) . '/../classes/class.Class1.php`;