PHP Not detecting class properlly - php

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!

Related

undefined function when function is present but late

I have this file structure in my php project:
splitted.php (ERROR)
<?php
require_once "include1.php";
require_once "include2.php";
include1.php
<?php
a();
include2.php
<?php
function a(){ echo "success"; }
It will gives error Uncaught Error: Call to undefined function a().
But if we combined them with the same exact sequence, it doesnt gives error.
combined.php (SUCCESS)
<?php
a();
function a(){ echo "success"; }
Why it gives error when we splitted the code but run without issue when we combine it? How to fix this error without altering the structure and the sequence of the file?

Class not found although it's in an included file

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.

variable out of scope when using include_once

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

Autoload error in php

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?

Fatal error: Call to undefined function tep_session_name()

I am trying to update one of the sites I maintain to the latest PHP and during this, I have come across the following error:
Fatal error: Call to undefined function tep_session_name() in ... /includes/application_top.php on line 83
The code it is referring to is:
// set the session name and save path
tep_session_name('osCAdminID');
tep_session_save_path(SESSION_WRITE_DIRECTORY);
But I have looked at the sessions.php file are the function is defined in the below code:
function tep_session_name($name = '') {
if ($name != '') {
return session_name($name);
} else {
return session_name();
}
}
Any help in identifying the cause would be greatly appreciated.
Thanks, E.
I'm absolutely sure, that you call this function before you include file with function declaration

Categories