I wrote an autoloader class.
But the include_once function does not include the file I need.
Cls.php(path:cls/)
<?php
class fileup{
function dup($a){
$x=strip_tags($a);
$d=__DIR__."/".$x.".cls.php";
echo $d; ///storage/emulated/legacy/htdocs/yeni/sinif/ayar.sinif.php
if(file_exists($d)){
include_once $d;
}else{
die($a." Class not found :(");
}
}
}
$dup=new fileup();
spl_autoload_register(array($dup,"dup"));
?>
Theme.cls.php Theme class (path:cls/)
<?php
include_once("cls.php");
$dup->dup("conf");
?>
Conf.cls.php Site config class (path:cls/)
<?php include_once("cls.php"); $dup->dup("db"); ?>
index.php (path:/)
<?php
include_once("theme.cls.php");
?>
Error details:
Fatal error: Uncaught Error:
Call to a member function dup() on null in /storage/emulated/legacy/htdocs/yeni/sinif/conf.cls.php:3
Stack trace:
#0 /storage/emulated/legacy/htdocs/yeni/sinif/cls.php(12):
**include_once() #1**
/storage/emulated/legacy/htdocs/yeni/sinif/theme.cls.php(3):
dup->dup('conf') #2
/storage/emulated/legacy/htdocs/yeni/index.php(2):
include_once('/storage/emulat...') #3
{main} thrown inĀ /storage/emulated/legacy/htdocs/yeni/sinif/conf.cls.php on lineĀ 3
Related
I am using
php 7.4.3
composer v4.1.4
I have this error
Fatal error: Uncaught Error: Call to undefined method Dotenv\Dotenv::createImmutable() in C:\xampp\htdocs\ecommerce\app\config\_env.php:9
Stack trace:
#0 C:\xampp\htdocs\ecommerce\bootstrap\init.php(8): require_once()
#1 C:\xampp\htdocs\ecommerce\public\index.php(3): require_once('C:\\xampp\\htdocs...')
#2 {main} thrown in C:\xampp\htdocs\ecommerce\app\config\_env.php on line 9
Define base path
_env.php ------------> any other method of defining realpath?
<?php
define('BASE_PATH', realpath(__DIR__.'/../../'));
require_once __DIR__.'/../../vendor/autoload.php';
$dotEnv = Dotenv\Dotenv::createImmutable(BASE_PATH);
$dotEnv->load();
index.php
<?php
require_once __DIR__ . '/../bootstrap/init.php';
$app_name = getenv('APP_NAME');
echo $app_name;
In my main page (index.php) I've included "class_lib.php" which contains the class definitions.
When calling the class from index.php - I'm getting the followin
Fatal error: Uncaught Error: Class 'person' not found in /home/latingate/public_html/test/ObejectOriented/index.php:5 Stack trace: #0 {main} thrown in /home/latingate/public_html/test/ObejectOriented/index.php on line 5
What am I doing wrong?
index.php
<?php include("class_lib.php"); ?>
<?php
$stefan = new person();
$jimmy = new person;
$stefan->set_name("Stefan Mischook");
$jimmy->set_name("Nick Waddles");
echo "Stefan's full name: " . $stefan->get_name();
echo "Nick's full name: " . $jimmy->get_name();
?>
class_lib.php
<?php
class person {
var $name;
function set_name($new_name) {
$this->name = $new_name;
}
function get_name() {
return $this->name;
}
}
?>
you can see it online here:
view online
Your code is correct and works for me.
I think Problems in file Path. So, first check your class_lib.php file is not in same directory then assign correct path.
For some reason it's not detecting the class files in the specified directory.
I'm unsure what the problem is here. Could anyone brighten this up for me?
Code:
<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
class main
{
public static
$test;
}
foreach(glob($_SERVER['DOCUMENT_ROOT'].'/engine/*.php') as $file) {
include($file);
}
main::$test = new Test();
?>
As you can see i'm trying to include all php files from /engine.php/*
Project file structure:
http://prntscr.com/hutqyo
But I'm getting the error
Fatal error: Uncaught Error: Class 'Test' not found in C:\xampp\htdocs\includes\loader.php:23 Stack trace: #0 {main} thrown in C:\xampp\htdocs\includes\loader.php on line 23
Here is my test.class.php code there isn't a lot in it yet. As i'm still working on this current problem:
<title>test</title>
<?php
class Test
{
function getIP(){
}
}
?>
Thanks to anyone that could possibly help me find the solution!
I am trying autoload function in php. The files are all in the same directory.
I have a file called aviation.php with the following code:
class Avaitor{
public function __construct(){
echo "Fly high<br>";
}
}
Then in my autoloader file I am trying to do this:
function __autoload($fileName){
if(file_exists($fileName . ".php"))
require_once $fileName . ".php";
}
//require_once "aviatior.php";
$pilot = new Avaitor();
But I am getting this error:
Fatal error: Uncaught Error: Class 'Avaitor' not found in
/Applications/MAMP/htdocs/php_oop/autoload.php:22 Stack trace: #0
{main} thrown in /Applications/MAMP/htdocs/php_oop/autoload.php on
line 22
Just to test that require_once does find my aviator.php I tried it out and then commented it out.
What am I doing wrong here?
This code:
<?php
namespace designblob;
function autoloader($class){
include "wrappers/databaseWrapper.php";
}
spl_autoload_register('autoloader');
?>
throws this error:
Fatal error: Uncaught exception 'LogicException' with message 'Function 'autoloader' not found (function 'autoloader' not found or invalid function name)' in /var/www/xxx/library/autoloader.php:8 Stack trace: #0 /var/www/xxx/library/autoloader.php(8): spl_autoload_register('autoloader') #1 /var/www/xxx/library/debug.php(19): require_once('/var/www/xxx...') #2 {main} thrown in /var/www/xxxm/library/autoloader.php on line 8
Why doesn't it work?
spl_autoload_register('designblob\autoloader');