Can somebody explain to me the concept of object scope in PHP? I'm very new to objects in PHP and the reason I ask is because I was able to create an object within an if statement, and then access the object outside the scope of the if statement.
Example:
//only create object if some condition is met
if ($conditionTrue){
$myBook = new Book('PHP for Dummies','softcopy');
}
$myBook.read();
I would have though that this would generate an error but it didn't.
Some background to my question
I was trying to figure out how to determine which constructor to call depending on the condition that is met. The only conceivable way was to introduce an if statement but doing that, I thought would impose the issue of scope which it didn't and I'm just wondering why..
In PHP, if doesn't have its own scope. So yes, if you define something inside the if statement or inside the block, then it will be available just as if you defined it outside (assuming, of course, the code inside the block or inside the if statement gets to run).For more about the PHP scope, read the variable scope manual page.
This scenario will generate error in other languages like JAVA,C#. But in PHP this is not going to happen.
Because in PHP we can create variable anywhere there is no need to first initialise the variable and after that assign the values in it.
In this scenario when you assigns the value to $myBook it first initialises the variable $myBook for global scope. So when you accessing the $myBook outside if block, it is already present in global scope of document and because of that you can access it without generating eror.
The above scenario has some limitation like where the variable is initialised, eg (within function, within class).
Related
I want to define a php variable that can be accessed in all the scripts and is initialized only one time per runtime. This is, like a static variable but with no object. I thought of using $GLOBALS but noticed the variables do not stick between script executions and I would have to instantiate the variable everytime. So how exactly can I accomplish this? Would I need to create an abstract class only to static the variable?
how about using $_SESSION? see http://www.w3schools.com/php/php_sessions.asp
What is the scope of a 'global' variable inside a module in Drupal? I created a module for a custom block and need to know how long the global stays set for so I know how to use them. Do they stay across the website instance, or only once for the page. For example in the following code drupal_set_message gets called once every time I load a page with the block. Can I be sure the 'global' variable is refreshed each time the block is loaded?
<?php
global $my_array;
function fill_array()
{
global $my_array;
if(!isset($my_array))
{
drupal_set_message("filling the array");
$my_array = array();
// code to fill array up...
}
}
Check out what documentation says:
http://php.net/manual/en/language.variables.scope.php
Global variables are by default available to all global code. But if you want to use them inside you function you have to declare them as global first.
And any kind php variable has lifetime no longer then one "page call". So with your next page call all the values are lost.
If you want to keep some value longer than single execution you must use php sessions or cookie or file or database....to store them there.
I know that this is an old question but scope is more tricky than this.
Drupal executes a block of PHP in a function context using eval() so variables defined at the top level within your PHP code are not in the global context unless they are explicitly set as global.
In other words, your code has three contexts, not the normal two.
In my test class I want a variable to be accessible from any test method. I know this is possible by initializing it in setUp method or declaring in bootstrap file.
But the issue is that the value of variable is unknown until test starts. It gets generated during a test and then used by subsequent test methods.
Currently I use this value by declaring subsequent methods to depend on the method, which generates the value, and then passing the value using return statement from method generating the value. But I don't think this is proper way as I have to add return statement just to make variable accessible elsewhere.
Is there any standard way to make variable accessible to every method which gets generated dynamically during test method execution?
Follow these steps:
1. Create a public class.
2. Declare the variable as global.
You can reuse the variable in any part of your application.
Coming from C++ I am used to be able to access class members directly in the body of their class, however, this doesn't seem to work in php - simple setters and getters fail to work unless explicitly using $this-> to access them. Setters seem to set to a temporary object that gets discarded and getters generate an error of trying to access non-existing objects.
Is there a way to directly access members inside the class body without the this keyword in php?
No, there is not. Setting an undefined variable will create it for the current scope, so that's what you're observing. (This is not a member variable though) - You can even read from an undefined variable, in which case the value will be null. This will generate an E_NOTICE though, so it's not considered good style.
Please read http://php.net/manual/en/language.oop5.php give a code snippet to see the exact problem you are facing
I need a way to see all the defined variables of the current PHP instance.
By currently defined I mean all the globals, all the local to THIS scope and all the locals to other scopes.
Is there something built in?
For everything in the current scope:
print_r(get_defined_vars());
I don't think there is a solution to dump all variables from all scopes, because you would have to actually run those functions/methods to get a complete and intact map of all available variables (variables could get created, added and removed at runtime).
You may have to look into Unit Testing or code coverage tools to see whether you can use anything from those areas, but it's going to be a lot of work.
What do you need this for?
var_dump($GLOBALS);
If a variable is not defined at the time this statement runs, then it is impossible to talk about it as having a value. Variables in "other" scopes don't exist.
{
$a = new myClass();
do stuff
$a->destroy();
}
print "$a has no meaning in this context";