Forbid specific variable access from global scope - php

Is there a way to forbid a variable from being picked out of global scope?
Something like:
#index.php
$forbidden = 'you should not be able to access me outside this scope'; // maybe invoke a function or namespaces?
require 'stuff.php';
echo new stuff();
#stuff.php
class stuff
{
public function __toString()
{
global $forbidden; // to result in an error or something?
/*
just a random example, but yes, any way
to somehow make a variable not global?
*/
return 'I am forbidden';
}
}
Have no idea if it's possible, but anyways, interested in OOP only fashion.
Reason?
To disallow a specific class from being instantiated into a variable and then taken out from global scope to reuse it's functions. Make class completely "private", kind of like a master class that does all the automation.
Hope I've made myself clear.

The only way I can think of would be to "fake" global scope...
//index.php
function scope() {
require 'actual.php';
}
scope();
and now, putting code in actual.php looks like "normal" code but it actually is inside function scope. Obviously you can't declare functions or classes in actual.php now, but otherwise it behaves the same, with the exception that any variables declared will be in the function's scope instead of global scope.
I really wouldn't do this though. A beating with a stick usually works if someone does something stupid like globals like that ;)

Related

php - can't access global variables from Thread?

I suppose this is a specific question, but for some reason, when I create a Thread like this:
require_once(__DIR__.'/myotherfile.php');
class StreamBufferInput extends Thread {
public function run(){
global $max_buffer_size;
global $data_source;
echo "DATA:" . $max_buffer_size;
...
}
}
myotherfile.php has those two variables declared in it (and they can be accessed from other classes, but my echo statement here prints DATA: and nothing else. I couldn't find much on doing global variables within classes, but I have a global declaration like this in a function of one of my other classes, and it works fine.
EDIT: Here is how I'm starting the Thread.
$stream = new StreamBufferInput();
$stream->start();
This is not possible in PHP at the moment. You cannot access global scope variables defined outside a thread from within the thread itself. However, you can execute a callable from within the thread, in the global scope by using Thread::globally, I believe this could help you achieve what you want.
You can read some more about this here

Defining a global variable, and including functions that require it?

I have six functions that require a global variable. In an attempt to reduce redundancy, I wrote a new function that is triggered rather than triggering all six. This one function has a global $var that is required by the other functions.
So, it looks like this
function one_function_for_the_rest() {
global $importantVar;
functionOne();
functionTwo();
functionThree();
}
but the global variable is not being seen by the called functions. Am I doing this incorrectly, or is this a limitation I was not aware of?
You're not doing it correctly as the variable is defined when on_function_for... is called. An easier way for this would be just to have $importantVar; at the start of the code.
Or wrap your functions inside a class and put the variable inside the class.
e: so basically do : function myFunc($important) { stuff } and when calling the function do myFunc($importantVar)
example :
$asd = "lol";
class myclass {
public function lol($asd) {
echo $asd;
}
}
$obj = new myclass;
$obj->lol($asd);
You're not doing it correctly. Each function either needs to use the global scope identifier, like global $importantVar;, or have $importantVar passed as a parameter. Otherwise, the other functions don't have $importantVar in their respective scopes.
Simply calling a function from within one_function_for_the_rest does not tell that other function anything about global variables or variables used in one_function_for_the_rest. In technical terms, your function calls do not bring $importantVar into the respective scopes of functionOne, functionTwo, or functionThree.
PHP does not have the same scoping rules as most other languages have. That is the downside to not having to declare variables with var as in JavaScript or other similar constructs.
Basically in PHP, every function used is only available in that function. There are three exceptions:
The global keyword
The $this variable inside objects. This one is also "magic" as it's also available inside anonymous functions defined inside a class.
When declaring an anonymous functions you can bind variables to it using use.

PHP set global variable as handle for class

For some reason, when defining:
function __construct() {
if(!isset($_GLOBALS["className"])) {
$_GLOBALS["className"] = new className;
}
return true;
}
$_GLOBALS["className"]->classMethod();
PHP for some reason states that $_GLOBALS["className"] is undefined.
Oh, and the same also occurs even if I set the global value to something else, from within that class. I can test the value of the global through the construct or some other method, but not outside - it seems that the global is lost outside the class for some reason.
Is there a way to retain the global after declaring it from within an external class?
Any help is sincerely appreciated!
You want to use $GLOBALS and not $_GLOBALS:
http://php.net/manual/en/reserved.variables.globals.php
$_GLOBALS will just be available in your function scope.

PHP Classes and global variables

I recently begun to use classes. I've been using procedural programming for quite a while so it has been a bit challenging.
My question.
If I have a class like this:
class Example {
public $name;
public $whatever;
public $yearAdded
public function __construct($name, $whatever=NULL, $dateAdded)
{
some trival code here;
}
}
How can I make $yearAdded use a global variable I set up somewhere else in another script?
FOR EXAMPLE:
global $currentYear = date('Y');
Would I have to do this way
new example($name, $whatever, $currentTime);
or is there a way to specify within the class to always use $currentYear for $yearAdded.
The global keyword doesn't work the way you think it does. It does not make a variable have global scope. All it does is specify to the function that you call it in that you want to use the variable in the outer scope.
For example:
$a="test";
function something() {
global $a;
echo $a; //Outputs: test
}
If you want to make a variable global so that it can be accessed from within a class, you need to use the $GLOBALS superglobal.
http://www.php.net/manual/en/reserved.variables.globals.php
This is not considered to be the best OOP way of doing things, but will get the job done.
You already have a builtin time() function, so why do you need a home-grown global variable in your constructor?
If you think carefully about how you are using globals, you'll find out that there are usually better ways of accessing the same info -- e.g. system environment variables, configuration files, etc.

Function scopes in regards with includes in PHP

I'm having a bit of trouble understanding includes and function scopes in PHP, and a bit of Googling hasn't provided successful results. But here is the problem:
This does not work:
function include_a(){
// Just imagine some complicated code
if(isset($_SESSION['language'])){
include 'left_side2.php';
} else {
include 'left_side.php';
}
// More complicated code to determine which file to include
}
function b() {
include_a();
print_r($lang); // Will say that $lang is undefined
}
So essentially, there is an array called $lang in left_side.php and left_side2.php. I want to access it inside b(), but the code setup above will say that $lang is undefined. However, when I copy and paste the exact code in include_a() at the very beginning of b(), it will work fine. But as you can imagine, I do not wish to copy and paste the code in every function that I need it.
How can I alleviate this scope issue and what am I doing wrong?
If the array $lang gets defined inside the include_a() function, it is scoped to that function only, even if that function is called inside b(). To access $lang inside b() you need to call it globally.
This happens because you include 'left_side2.php'; inside the include_a() function. If there are several variables defined inside the includes and you want them to be at global scope, then you will need to define them as such.
Inside the left_side.php, define them as:
$GLOBALS['lang'] = whatever...;
Then in the function that calls them, try this:
function b() {
include_a();
print_r($GLOBALS['lang']); // Now $lang should be known.
}
It is considered 'bad practice' to use globals where you don't have to (not a consideration I subscribe to, but generally accepted). The better practice is to pass by reference by adding an ampersand in front of the passed variable so you can edit the value.
So inside left_side or left_side2 you would have:
b($lang);
and b would be:
function b(&$lang){...}
For further definitions on variable scopes check this out

Categories