last day (14 days later) » 

12:59 AM
0
Q: Why require-once() doesn't work?

stackI have this folder-structure: -out -DBconnection.php -root -application -contact.php Also here is the content of contact.php: function index(){ <form action="http://example.com/localhost/application/contact/check"> // some code here ... </form> } function chec...

 
It's not possible that require "works" but require_once does not. What does that "does not work" actually mean?
 
@zerkms When I use require_onec that database connection script doesn't include. And I gives me this error Fatal error: Call to a member function prepare() on null in ..
 
And how that fatal error is relevant to the require_once?
 
@zerkms When I use require that fatal error hides. Also when I use require_once there is a Notice: Undefined variable: db_con in ...
 
You requested a file to be required only once - so it was required only once.
 
12:59 AM
@claudios doesn't work ...
 
Sure it does not - require_once requires a file once (as it follows from its name). It works perfectly fine, exactly as requested.
 
@zerkms I think my the problem comes form my routeing system. I really don't know why require() works but require() doesn't.
 
@stack it WORKS. It does exactly as it should, as per documentation. require_once requires a file ONCE per script run. So every other attempt is simply not performed.
 
@claudios It works require __DIR__."/../../inaccessible/dbh_conn.php";
@zerkms I don't get it ..! don't be angry, nevermind.., That was just odd for me..! :-)
 
What exactly don't you get? require_once requires just once. So every other call is simply skipped.
 
12:59 AM
@zerkms I just need that database-connection one time in my script. not more.
 
So create a function that returns you a connection. Useful keyword for the case: static
 
@zerkms Got it. Thank you. Just as my last question, Why dirname(dirname(dirname(__FILE__)))."/out/DBconnection.php" works, but "../../../out/DBconnection.php" doesn't work?
 
Because ../.. path is relative to the current directory, and the other one is absolute. Check the current directory with getcwd()
 
@zerkms WTF ... You are the best .. I love you buddy ..! Almost you are the only one who is able to read my mind and answer me :-)
 

last day (14 days later) »