Today I tried to include a file that returns an object. I always use require_once, but now I noticed its strange behavior.
Main.php file
$lang = false; $lang->name = "eng"; $lang->author = "Misiur"; $lang->text = "Text is test"; $lang->undefined = "Undefined"; return $lang;
Index.php file
$lang = include('langs/eng/main.php'); var_dump($lang); echo "<br />"; $lang = require('langs/eng/main.php'); var_dump($lang); echo "<br />"; $lang = require_once('langs/eng/main.php'); var_dump($lang);
Result
object(stdClass)#9 (4) { ["name"]=> string(3) "eng" ["author"]=> string(6) "Misiur" ["text"]=> string(12) "Text is test" ["undefined"]=> string(9) "Undefined" } object(stdClass)#10 (4) { ["name"]=> string(3) "eng" ["author"]=> string(6) "Misiur" ["text"]=> string(12) "Text is test" ["undefined"]=> string(9) "Undefined" } bool(true)
Why is that? I thought that require and require_once are the same thing, only require_once is more secure because it will not duplicate include.
Thanks.
Edit:
But when I use only require_once, I also get bool (true). So require_once returns only the result of the include, not the content?
Edit2:
Lol. I did not notice that earlier I needed this file inside my class, which was created before this code execution ($ this-> file = require_once ("langs / $ name / main.php");)
So require_once worked as it should. Thanks guys!
object include php require require-once
Misiur
source share