I have an array with associated keys that point to instantiated objects. For example:
$MyArray = array();
$MyArray['Object_A'] = new Object_A();
$MyArray['Object_B'] = new Object_B();
$MyArray['Object_C'] = new Object_C();
What I would like to do with this array is to extract it to variables as references to those objects.
extract($MyArray, EXTR_REFS);
The statement works and I am able to use those objects inside that array just as I would do $Var = new Object();.
$Object_A->SomeMethod();
However when I extract them in a function that I define they can no longer be used outside that function. They can be used inside the function like the example below but not outside.
function ExtractObj(&$Array)
{
extract($Array, EXTR_REFS);
$Object_A->SomeMethod(); // This works.
}
So I need a way to make the extracted variables from that function to be usable from outside the function.
function ExtractObj(&$Array)
{
extract($Array, EXTR_REFS);
}
$Object_A->SomeMethod(); // Not working (Yet).
NOTE: I have tried to use global, static variable modifiers or methods and so on but everything gives me an error.
Yes, you are correct. Variables have function scope, so they are not available outside the function. Learn to pass variables into a function as parameters, return needed values from the function and live with the function scope, it will make your programs better and more maintainable than one big messy global scope.
Related
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.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Access global variable from within a class
I want to fortify my knowledge in PHP. But most of the time I have trouble understanding with scopes. I am not sure when to use $this and global declaration keyword on my functions.
This is just an example class I just omitted the __construct()
class myClass{
public myVariable;
public function1() {
$this->myVariable=1;
}
public function2(){
global $myVariable;
$myVariable=1;
}
}
Which one is the best approach to use when assigning pre-declared variables inside a function? I am confused somehow by the different books by major publishers in PHP. I am not sure if I am asking the correct question or somehow relevant.
First off, thats not valid PHP, as Jared Farrish already said. Its public $myvar instead of public myvar. Variable names always begin with $.
When you declare a variable in a class:
<?php
class A
{
private $var;
}
That variable will be automatically available in all methods if accessed through $this (unless the method static, but that is another story). So this would work:
<?php
class A
{
private $var;
public function foo () {
// This works
$this->var = 1;
// This is a completely different thing and does not change
// the contents of A::$var
$var = 1;
}
}
Now global is a different thing altogether. In PHP, you cant do this:
<?php
$global_a = 123;
function myfunc ()
{
// You wont actually change the value of $global_a with this
$global_a = 1234;
}
echo $global_a; // still 123
You'd think this would work, but global variables aren't automatically available to functions. This is where global comes in:
<?php
$global_a = 123;
function myfunc ()
{
global $global_a;
$global_a = 1234;
}
echo $global_a; // now it will be 1234
I suggest you read about variable scope in PHP and then you can go on to OOP in PHP.
PHP is a very quirky language. Just because something works in most languages in a certain way doesn't mean it will work in PHP. Most of the times, it wont. So its best to educate yourself as much as possible.
Hope this helps
Which one is best often depends on the situation. In general it's best to avoid global variables as they bring risks with them of not knowing exactly where they're used.
Using a class variable is a good option if the variable is specific to that instance of the class. (You will have to prefix it with a $, though. I.e., public $myVariable;).
If the variable is only relevant to the function and instances outside the class, you'd do best to pass it as an reference. This is done by adding an & before the $ in the parameter space. I.e.:
public function3(&$myVariable) {
$myVariable = 1;
}
Or alternatively just return the value you wish and set it outside the function. I.e.,:
class MyClass {
public function3() {
return 1;
}
}
$myObject = new MyClass();
$myVariable = $myObject->function4();
try to avoid passing global variable,as there are many good reasons for that:
Reusing parts of the script is impossible :
If a certain function relies on global variables, it becomes almost impossible to use that function in a different context. Another problem is that you can’t take that function, and use it in another script.
Solving bugs is much harder :
Tracking a global variable is much harder than a non-global variable.
Understanding the code in a year will be much more difficult :
Globals make it difficult to see where a variable is coming from and what it does.
Now your questions:
Which one is the best approach to use when assigning pre-declared variables inside a function?
You can pass them by value or reference inside a function
By value
$firstVar = 1;
function abc($firstVar){
echo $firstVar ; // will give you 1
}
By reference
function abc(&$firstVar){
$firstVar = 3; // this will give utility to change that variable even
echo $firstVar ; // will give you 3
}
Now where to use $this variable:
$this is used to refer to the current object.
example:
class abc{
private $firstVar;
function abc(){
$this->firstVar = 2;
}
}
i have to function in two different files.
one of them should add a new item to an array each time is called and the array should be accessible .what i did for it is :
function1(){
global $array;
$array[] = 'hi';
}
but it just create one item in array even if i call this function 4 times .
What you did should work.
<?php
function function1(){
global $array;
$array[] = 'hi';
}
function1();
function1();
function1();
print_r($array);
Test it.
You probably have another problem. Please note that the lifetime of all variables is the current run of your script. They won't exist in a successive run. For that you need to use some sort of persistence like session, cookie, file system, database.
For more help post your complete code.
I'm a bit confused by the wording of your question. When you say "i have to function in two different files." does you mean you have "two" functions?
If you have two functions both trying to use your $array variable, you'll need to call global $array; in both functions.
The reason for this is that global is a bit misleading. All it's really doing is assigning a reference to a member of $_GLOBALS to a variable in the local scope which has the same name as the $_GLOBALS index. In other words, if you do something like this:
global $variable;
it's essentially the same thing as saying this:
$variable =& $_GLOBALS['variable']; (assign by reference)
The actual variable $variable is still scoped at the function level, it just happens to have a reference to a global variable.
The implication of this is that if you don't define global $variable in every function, you're just creating a brand new variable within the scope of that function. When the function ends, the variable is unset and any changes made to it within the function are lost.
With all of that said, global variables still tend to be a bad idea. It's a lot clearer if you just maintain a local variable, and pass it as a parameter to other functions when needed.
I had the problem that a global $array was not known in a function. But when I placed the first def: $array = array(); before the first function call, it worked.
i've been thinking if something like this is possible.
// this creates a variable $test in the scope it was called from
function create_var() {}
class A {
function test()
{
create_var();
// now we have a local to var() method variable $test
echo $test;
}
}
So, the question is, can a function create_var() create a variable outside of its scope, but not in a global scope? Example would be the extract() function - it takes an array and creates variables in the scope it was called from.
Nope, this is not possible. It's possible only to access the global scope from within a function.
You could make create_var() return an associative array. You could extract() that in your function:
function create_var()
{ return array("var1" => "value1", "var2" => "value2"); }
class A {
function test()
{
extract(create_var());
// now we have a local to var() method variable $test
echo $test;
}
}
Something a bit closer to what you want to do is possible in PHP 5.3 using the new closures feature. That requires declaring the variables beforehand, though, so it doesn't really apply. The same goes for passing variable references to create_var(): create_var(&$variable1, &$variable2, &$variable3....)
A word of warning: I can think of no situation where any of this would be the best coding practice. Be careful when using extract() because of the indiscriminate importing of variables that it performs. It is mostly better to work without it.
Im trying to make a simple template system in PHP. I'm new to PHP, so it's nothing serious. I have some problems though:
A regular include works:
$variable = "test";
include("templates/news.html");
But this won't:
This says $variable is undefined:
$variable = "test";
getTemplate("news");
The Function:
function getTemplate($tpl) {
$file = "templates/$tpl.html";
if (file_exists($file))
return include($file);
return false;
}
news.html
<h1>php echo $variable</h1>
the function works and includes the page but it dont write out the variables
I include the function on top of all pages.
Thanks in advance!
With the extract function, you can define different variables from an array.
You can make it like this:
$vars = array('var1' => "value1", 'var2' => "value2");
function getTemplate($tpl, $vars) {
$file = "templates/$tpl.html";
extract($vars, EXTR_SKIP)
if (file_exists($file))
return include($file);
return false;
}
getTemplate('news', $vars);
In your template, you can use $var1 and $var2.
you are trying to reach global variable in a function.
you must either declare a variable global in a function or just use: $GLOBALS["variable"]
function () {
global $variable;
etc..
}
Because you're including the file from inside a function, the contents of that file are no longer in the global scope.
You either need to add global $variable; to the start of the function, or use echo $GLOBALS['variable']; in news.html.
I believe your problem has to do with variable scoping. When you call a function, you are unable to access variables outside of that function, except global variables (more on this in a second) or variables passed to your function. The global function tells PHP that you'd like to use the same variable in the function as in the parent scope. You can also use the define function to define constants for your script, which are universally accessed.
Using large number of global variables is very frowned upon, I'd suggest passing either an associative array to the function, or use a more object oriented approach.
Hope this helps,
Jacob
Have you considered using Smarty
If you don't want to use this template engine maybe you should check some of the ideas behind it. In short, it deals with assigning variables to templates by assigning variable values to an object. If you assigned variable to a template object you can then use it in template file.