I've researched this and found other questions (PHP class not found but it's included) but they don't answer this stupidly simple problem!
Here's the error: Class 'Functions' not found in /public_html/sites/sitename/index.php
My folder structure is like this:
public_html/assests/php/functions.php //class
I require_once this file from here: public_html/sites/sitename/index.php
I've checked the file_exists, of course it does else require_once would fail. What is going on?!
Here's my code (just in case I've missed something really dumb...)
require_once('../../assets/php/functions.php');
if( file_exists('../../assets/php/functions.php') ){
error_log('Of course it exists!');
}
else{
error_log('Oh dear...');
}
$fn = new Functions();
Contents of functions.php (edited for brevity):
class Functions{
public function functions(){
//constructor
}
//pretty print
public function pp($str){
return '<pre class="pre-show prettyprint">'.htmlentities($str).'</pre>';
}
}
You have a misspelling, an extra s in 'assets':
My folder structure is like this:
public_html/assests/php/functions.php //class
Make sure your file name is "assets" and make sure your require matches that:
require_once('../../assets/php/functions.php');
Related
I want to make my own class in php to handle my errors I want to show to the user, for example when the user are logging in but the password or username is false.
I have a map classes with the files:
errorHandling.class.php
class errorHandling
{
public $customError;
public function setCustomError($error)
{
$this->customError = $error;
}
public function getCustomError()
{
echo $this->customError;
}
}
And in the users.class.php
when the user try's to login and it fails the else will be like:
else {
include 'errorHandling.class.php';
$errorHandle = new errorHandling();
$errorHandle->setCustomError("Username or password are wrong!");
}
Then in the login.php in the root map I have this code to call the function:
include 'classes/errorHandling.class.php';
include 'classes/users.class.php';
$errorHandle = new errorHandling();
$errorHandle->getCustomError();
Well now I get this error message but I don't understand it so I hope some of you guys can helping me out or give me some tips to improve my class.
Fatal error: Cannot declare class errorHandling, because the name is already in use in /classes/errorHandling.class.php on line 1
Every time you include a file in php, it will be loaded and run. For class definitions, you want to use require_once, which as it says, will only be loaded once.
I am developing an app where I need to log the proccess.
So I was loggin from main.php and now I need to log from another class (class_TestingLog.php)
Next code is how I am trying. Could you tell me what I am doing wrong?
Thank you in advance.
main.php
[...]
#Logging class
include_once("./classes/class_log.php");
$Class_Log = New Class_Log();
#TestingLog Class
include_once("./classes/class_testingLog.php");
$Class_TestingLog = New Class_TestingLog();
[...]
$Class_Log->FreeLog("Test log");
$Class_TestingLog->AnyFuncion();`
[...]
class_log.php
[...]
public function FreeLog($text)
{
// echo $text . "\n"; #mistake
$this->outputText .= text; #correct one
}
[...]
class_testingLog.php
private $Class_Log = '';
[...]
public function __construct()
{
#Requires
require_once("./classes/class_log.php");
$this->Class_Log = New Class_Log();
}
public function AnyFuncion()
{
var_dump($this); #From real file
$this->Class_Log->FreeLog("Test log from another classs");
}
[...]
Browser output
Test log
Browser output expected
Test log
Test log from another classs
===========================================================================
EDIT
I made an error copying FreeLog();
It stores the parameter string value into a private variable instead echo the variable.
You require_once statement inside __construct for Class_TestingLog is invalid and unnecessary. As both files are in the same directory it should be "class_log.php" not "./classes/class_log.php". There is no need for it anyway, as when you include them both in main.php it is loaded already. So try it without the require_once.
EDIT: As discussed in chat do it like this:
in main.php:
$Class_TestingLog = New Class_TestingLog($Class_Log);
in class_testingLog.php:
public function __construct($ClassLog)
{
$this->Class_Log = $ClassLog;
}
This will use the same instance inside the Class_TestingLog class as on the main.php.
I am creating website in PHP. I am using MVC in PHP. My website works like this, if user go to example.com/about then it it will load About class and index() function. If user will go to localhost/about/founder then it will load founder() function from About class. but the thing is that if I go to localhost/About or localhost/AbOut or anything like that it is loading default index() function from About class file. So what to do with case sensitivity? I mean I want my script to load index() function from class file if it is localhost/about or localhost/terms. If anything is in uppercase, then it should load 404 error function. 404 error function is already set in my site.
Please help me friends.
here is my Bootstrap.php class file
<?php
/*
Bootstrap class to run functions by URL
*/
class Bootstrap {
public $_req;
public $_body;
public $_file;
public $_error;
function __construct(){
if(empty($_GET['req'])){
require 'classes/home.php';
$this->_body = new Home();
$this->hdr($this->_body->head());
$this->_body->index();
$this->ftr();
exit();
}
$this->_req = rtrim($_GET['req'], '/');
$this->_req = explode('/', $this->_req );
$_file = 'classes/'.$this->_req[0].'.php';
if(file_exists($_file)){
require $_file;
}
else {
$this->error(404);
}
$this->_body = new $this->_req[0];
$this->hdr($this->_body->head());
if(isset($this->_req[2])){
if(method_exists($this->_req[0], $this->_req[1])){
$this->_body->{$this->_req[1]}($this->_req[2]);
}else {
$this->error(404);
}
}else {
if(isset($this->_req[1])){
if(method_exists($this->_req[0], $this->_req[1])){
$this->_body->{$this->_req[1]}();
}else {
$this->error(404);
}
}else {
$this->_body->index();
}
$this->ftr();
}
}
//this function is to set header in html code
public function hdr($var = false){
echo '<!DOCTYPE HTML><html><head>'.$var.'</head><body>';
}
//this function is tp set footer in html code
public function ftr($var = false){
echo $var.'</body></html>';
}
//error handler
public function error($var){
require 'classes/er_pg.php';
$this->_error = new Error();
$this->_error->index($var);
}
}
You shouldn't use anything to load non-lowercase URLs because of the duplicate content, and that's a good thing you're doing. The wrong URLs should fail automatically in such cases.
However, since you didn't show how are you making those calls, then only thing I can suggest at this point is to check if the called method exists (case-sensitive), and if not, throw/redirect to a 404 page (header($_SERVER["SERVER_PROTOCOL"]." 404 Not Found");).
UPDATE
After all the chat in the comments, seems like file_exists is not case-sensitive in your case, which is really weird. Hopefully someone will be able to figure it out so I can delete this (keeping it because of the info in the comments).
I solved the problem. I used this
if(ctype_lower($this->_req[0])){
$_file = 'classes/'.$this->_req[0].'.php';
and now its working. Thanx anyways friends.
I don't get why I get this error
Fatal error: Class 'ImageJpg' not found
Here is the code that I use
spl_autoload_register(function($class) {
if(file_exists($class))
{
include $class.'.php';
}
});
$n = new ImageJpg();
File ImageJpg.php is in the same dir with the code above.
Here is the content of ImageJpg.php
<?php
class ImageJpg
{
public function __construct()
{
echo 'Image from jpg called';
}
}
if(file_exists($class))
{
include $class.'.php';
}
Should be
if(file_exists($class.'.php'))
{
include $class.'.php';
}
Is there a class named ImageJpg in your ImageJpg.php file? And does file exist?
Try this:
spl_autoload_register(function($class) {
if(file_exists($class.'.php'))
{
include $class.'.php';
if (!class_exists($class))
{
die('required class not present');
}
}
else
{
die('file not found');
}
});
I had issues with file_exists also and I was debugging for like 2 hours and nothing happen even after I've searched all around and also on web. At the end it was a typo: instead userController.php it was userControlller.php the actual file.
Check again (in this order):
Make SURE you don't have typo in your file name (either PHP or file system)
Check your path - ROOT and/or use getcwd()
Temporary print / log your checks to make sure everything matches
Avoid human error :)
I understand that the question is rather hard to understand, I didn't know how to ask it better, so I'll use this code example to make things more clear:
If I have the following files:
test.php:
<?php
include('include.php');
echo myClass::myStaticFunction();
?>
include.php
<?php
__autoload($classname){
include_once("class/".$classname.".php"); //normally checking of included file would happen
}
?>
class/myClass.php
<?php
class myClass{
public static function myStaticFunction(){
//I want this to return test.php, or whatever the filename is of the file that is using this class
return SOMETHING;
}
?>
the magic FILE constant is not the correct one, it returns path/to/myClass.php
in case you need to get "test.php" see $_SERVER['SCRIPT_NAME']
I ended up using:
$file = basename(strtolower($_SERVER['SCRIPT_NAME']));
I am using
$arr = #debug_backtrace(false);
if (isset($arr))
foreach ($arr as $data)
{
if (isset($data['file']))
echo $data['file'];
// change it to needed depth
}
This way you don't need to modify the file from which your file is included. debug_backtrace might have some speed consenquencies.