Autoload is changing Scope of variables included in autoloaded file
I'm having some trouble with my php project...
The autoload function:
spl_autoload_register(function ($class_name) {
include 'HTMLClasses/'.$class_name . '.php';});
When autloloading classes from the "HTMLClasses" directory, the variables in the autoloaded file which are included from other files are not in its scope anymore.
someFile.php
require_once $_SERVER["DOCUMENT_ROOT"] . "/config/phpFileIndex.php";
require_once $phpFile['resourcesPaths'];
include_once "../autoload.php";
class SomeClass{}
the problem now is, that when autoloading this class the $phpFile Variable isn't in the scope of this script anymore. It is Global and i can access it by using $GLOBALS but I do not understand why I need this at all, beacause if I include SomeClass manualy, the variable is in the normal scope. In the documentation of spl_autoload_register there is nothing documented about this behavior.
Another problem is that i can't include autoload from here. Even though the autoload.php is in the upper directory. When including the full path from Document_root it works but not relative. And I actually do not understand this. The warning says:
'Warning: include_once(): Failed opening '../autoload.php' for inclusion (include_path='xamppphpPEAR') in F:<path>HTMLClassesTemplate.php on line 4'
I dont know if this is a problem with the code or with my enviorment.
I tried searching for it, but the problem is actually, that I don't know what to search for. If anyone has an Idea about this I would really apreciate it. :)
php scope include autoload
add a comment |
I'm having some trouble with my php project...
The autoload function:
spl_autoload_register(function ($class_name) {
include 'HTMLClasses/'.$class_name . '.php';});
When autloloading classes from the "HTMLClasses" directory, the variables in the autoloaded file which are included from other files are not in its scope anymore.
someFile.php
require_once $_SERVER["DOCUMENT_ROOT"] . "/config/phpFileIndex.php";
require_once $phpFile['resourcesPaths'];
include_once "../autoload.php";
class SomeClass{}
the problem now is, that when autoloading this class the $phpFile Variable isn't in the scope of this script anymore. It is Global and i can access it by using $GLOBALS but I do not understand why I need this at all, beacause if I include SomeClass manualy, the variable is in the normal scope. In the documentation of spl_autoload_register there is nothing documented about this behavior.
Another problem is that i can't include autoload from here. Even though the autoload.php is in the upper directory. When including the full path from Document_root it works but not relative. And I actually do not understand this. The warning says:
'Warning: include_once(): Failed opening '../autoload.php' for inclusion (include_path='xamppphpPEAR') in F:<path>HTMLClassesTemplate.php on line 4'
I dont know if this is a problem with the code or with my enviorment.
I tried searching for it, but the problem is actually, that I don't know what to search for. If anyone has an Idea about this I would really apreciate it. :)
php scope include autoload
Tryrequire_once __DIR__ . '/../autoload.php';
. You need to refer to the__DIR__
when the path is relative to the current file (otherwise, it will be relative to the currently executed script, which might not be the same).
– Jeto
Nov 19 '18 at 19:20
Oh boy... Completly forgot about this... Thank you very much. :)
– CKWDani
Nov 19 '18 at 19:25
1
The scope of the variables are now within the scope of your anonymous function. I would either make these properties of the class, or find some other way to access those variables that is not referencing globals. If these variables are static, make them class constants. If their values change, make them properties.
– user9189147
Nov 19 '18 at 19:27
Ah ok I understand... But $phpFile is not in an class. Its just written in the script. And I call the require from the someFile.php and not from somewhere else. So when Including phpFileIndex.php the scope of the variables of this file should be in the scope of the phpFile, doesn't it? Because I don't call the variable from outsige but in the next line....
– CKWDani
Nov 19 '18 at 19:37
add a comment |
I'm having some trouble with my php project...
The autoload function:
spl_autoload_register(function ($class_name) {
include 'HTMLClasses/'.$class_name . '.php';});
When autloloading classes from the "HTMLClasses" directory, the variables in the autoloaded file which are included from other files are not in its scope anymore.
someFile.php
require_once $_SERVER["DOCUMENT_ROOT"] . "/config/phpFileIndex.php";
require_once $phpFile['resourcesPaths'];
include_once "../autoload.php";
class SomeClass{}
the problem now is, that when autoloading this class the $phpFile Variable isn't in the scope of this script anymore. It is Global and i can access it by using $GLOBALS but I do not understand why I need this at all, beacause if I include SomeClass manualy, the variable is in the normal scope. In the documentation of spl_autoload_register there is nothing documented about this behavior.
Another problem is that i can't include autoload from here. Even though the autoload.php is in the upper directory. When including the full path from Document_root it works but not relative. And I actually do not understand this. The warning says:
'Warning: include_once(): Failed opening '../autoload.php' for inclusion (include_path='xamppphpPEAR') in F:<path>HTMLClassesTemplate.php on line 4'
I dont know if this is a problem with the code or with my enviorment.
I tried searching for it, but the problem is actually, that I don't know what to search for. If anyone has an Idea about this I would really apreciate it. :)
php scope include autoload
I'm having some trouble with my php project...
The autoload function:
spl_autoload_register(function ($class_name) {
include 'HTMLClasses/'.$class_name . '.php';});
When autloloading classes from the "HTMLClasses" directory, the variables in the autoloaded file which are included from other files are not in its scope anymore.
someFile.php
require_once $_SERVER["DOCUMENT_ROOT"] . "/config/phpFileIndex.php";
require_once $phpFile['resourcesPaths'];
include_once "../autoload.php";
class SomeClass{}
the problem now is, that when autoloading this class the $phpFile Variable isn't in the scope of this script anymore. It is Global and i can access it by using $GLOBALS but I do not understand why I need this at all, beacause if I include SomeClass manualy, the variable is in the normal scope. In the documentation of spl_autoload_register there is nothing documented about this behavior.
Another problem is that i can't include autoload from here. Even though the autoload.php is in the upper directory. When including the full path from Document_root it works but not relative. And I actually do not understand this. The warning says:
'Warning: include_once(): Failed opening '../autoload.php' for inclusion (include_path='xamppphpPEAR') in F:<path>HTMLClassesTemplate.php on line 4'
I dont know if this is a problem with the code or with my enviorment.
I tried searching for it, but the problem is actually, that I don't know what to search for. If anyone has an Idea about this I would really apreciate it. :)
php scope include autoload
php scope include autoload
edited Nov 19 '18 at 19:28
CKWDani
asked Nov 19 '18 at 19:14
CKWDaniCKWDani
11
11
Tryrequire_once __DIR__ . '/../autoload.php';
. You need to refer to the__DIR__
when the path is relative to the current file (otherwise, it will be relative to the currently executed script, which might not be the same).
– Jeto
Nov 19 '18 at 19:20
Oh boy... Completly forgot about this... Thank you very much. :)
– CKWDani
Nov 19 '18 at 19:25
1
The scope of the variables are now within the scope of your anonymous function. I would either make these properties of the class, or find some other way to access those variables that is not referencing globals. If these variables are static, make them class constants. If their values change, make them properties.
– user9189147
Nov 19 '18 at 19:27
Ah ok I understand... But $phpFile is not in an class. Its just written in the script. And I call the require from the someFile.php and not from somewhere else. So when Including phpFileIndex.php the scope of the variables of this file should be in the scope of the phpFile, doesn't it? Because I don't call the variable from outsige but in the next line....
– CKWDani
Nov 19 '18 at 19:37
add a comment |
Tryrequire_once __DIR__ . '/../autoload.php';
. You need to refer to the__DIR__
when the path is relative to the current file (otherwise, it will be relative to the currently executed script, which might not be the same).
– Jeto
Nov 19 '18 at 19:20
Oh boy... Completly forgot about this... Thank you very much. :)
– CKWDani
Nov 19 '18 at 19:25
1
The scope of the variables are now within the scope of your anonymous function. I would either make these properties of the class, or find some other way to access those variables that is not referencing globals. If these variables are static, make them class constants. If their values change, make them properties.
– user9189147
Nov 19 '18 at 19:27
Ah ok I understand... But $phpFile is not in an class. Its just written in the script. And I call the require from the someFile.php and not from somewhere else. So when Including phpFileIndex.php the scope of the variables of this file should be in the scope of the phpFile, doesn't it? Because I don't call the variable from outsige but in the next line....
– CKWDani
Nov 19 '18 at 19:37
Try
require_once __DIR__ . '/../autoload.php';
. You need to refer to the __DIR__
when the path is relative to the current file (otherwise, it will be relative to the currently executed script, which might not be the same).– Jeto
Nov 19 '18 at 19:20
Try
require_once __DIR__ . '/../autoload.php';
. You need to refer to the __DIR__
when the path is relative to the current file (otherwise, it will be relative to the currently executed script, which might not be the same).– Jeto
Nov 19 '18 at 19:20
Oh boy... Completly forgot about this... Thank you very much. :)
– CKWDani
Nov 19 '18 at 19:25
Oh boy... Completly forgot about this... Thank you very much. :)
– CKWDani
Nov 19 '18 at 19:25
1
1
The scope of the variables are now within the scope of your anonymous function. I would either make these properties of the class, or find some other way to access those variables that is not referencing globals. If these variables are static, make them class constants. If their values change, make them properties.
– user9189147
Nov 19 '18 at 19:27
The scope of the variables are now within the scope of your anonymous function. I would either make these properties of the class, or find some other way to access those variables that is not referencing globals. If these variables are static, make them class constants. If their values change, make them properties.
– user9189147
Nov 19 '18 at 19:27
Ah ok I understand... But $phpFile is not in an class. Its just written in the script. And I call the require from the someFile.php and not from somewhere else. So when Including phpFileIndex.php the scope of the variables of this file should be in the scope of the phpFile, doesn't it? Because I don't call the variable from outsige but in the next line....
– CKWDani
Nov 19 '18 at 19:37
Ah ok I understand... But $phpFile is not in an class. Its just written in the script. And I call the require from the someFile.php and not from somewhere else. So when Including phpFileIndex.php the scope of the variables of this file should be in the scope of the phpFile, doesn't it? Because I don't call the variable from outsige but in the next line....
– CKWDani
Nov 19 '18 at 19:37
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53381192%2fautoload-is-changing-scope-of-variables-included-in-autoloaded-file%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53381192%2fautoload-is-changing-scope-of-variables-included-in-autoloaded-file%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Try
require_once __DIR__ . '/../autoload.php';
. You need to refer to the__DIR__
when the path is relative to the current file (otherwise, it will be relative to the currently executed script, which might not be the same).– Jeto
Nov 19 '18 at 19:20
Oh boy... Completly forgot about this... Thank you very much. :)
– CKWDani
Nov 19 '18 at 19:25
1
The scope of the variables are now within the scope of your anonymous function. I would either make these properties of the class, or find some other way to access those variables that is not referencing globals. If these variables are static, make them class constants. If their values change, make them properties.
– user9189147
Nov 19 '18 at 19:27
Ah ok I understand... But $phpFile is not in an class. Its just written in the script. And I call the require from the someFile.php and not from somewhere else. So when Including phpFileIndex.php the scope of the variables of this file should be in the scope of the phpFile, doesn't it? Because I don't call the variable from outsige but in the next line....
– CKWDani
Nov 19 '18 at 19:37