Fatal error: Class 'Connection' not found in D:\Projects\wamp\www\Session-6\classes\header.php on line 4

亡梦爱人 提交于 2021-01-28 08:19:57

问题


Here I am trying to include a class file into another class file like

<?php
include './connection.php';
class header_class  extends connection
{

}

and in connection.php

<?php

class connection {
    //put your code here
}

both the files are in same folder called classes now when i am trying to extend connection class in header_class. Its give an fatal error.

( ! ) Fatal error: Class 'Connection' not found in D:\Projects\wamp\www\Session-6\classes\header.php on line 4
Call Stack
#   Time    Memory  Function    Location
1   0.0004  252240  {main}( )   ..\index.php:0
2   0.0008  267688  include( 'D:\Projects\wamp\www\Session-6\header.php' )  ..\index.php:8
3   0.0010  276552  include_once( 'D:\Projects\wamp\www\Session-6\classes\header.php' ) ..\header.php:2

i am quite surprised why it is happening..not only this but when i write connection class in same file as header_class.php ..Holaa its working..

please can anyone have idea why this is happening ?

UPDATE : here i try something like create a subfolder in classes like temp put the connection.php file in it and then try to include its working !!!!!!!!!!!!! how ???


回答1:


Replace this include './connection.php'; to include 'connection.php';
Replace you class name connection to Connection




回答2:


The reference path to any include file is referenced from your entry point. In your case, it is index.php.

So instead of including connection.php, you should include classes/connection.php to make it work.

P.S. If you follow PSR4, you can simply use composer to auto load your class files. No need to deal with include path issue. Worth learning it.




回答3:


You probably spelled connection wrong in " extends connection "...

Make sure your typing is correct, and make sure you save before testing your code..




回答4:


finally after googling i found this

include __DIR__. '/connection.php';

its working but don't why i need to append this __DIR__. any idea ?



来源:https://stackoverflow.com/questions/42336839/fatal-error-class-connection-not-found-in-d-projects-wamp-www-session-6-clas

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!