"global" does not work in CakePHP 2.1.1 views - php

I am using PHP 5.3.8 with CakePHP 2.1.1.
This is my view (the layout is empty, actually it only outputs the view itself)
<?php
// $present is not a view variable
$present = 'Hello World!';
class ApplicationsPDF
{
public function CreateApplicationTable()
{
global $present;
exit(var_dump($present));
}
}
$pdf = new ApplicationsPDF();
$pdf->CreateApplicationTable();
?>
The output is null instead of "Hello World!".
If I copy and paste this code into a single file (which I directly run from the browser), it perfectly works!
So it must be a CakePHP bug. Does anyone know it?

Try to declare the global keyword before the class definition:
global $present;
class ApplicationsPDF
{
public function CreateApplicationTable()
{
exit(var_dump($present));
}
}

It's not a PHP nor a CakePHP bug!
It's because CakePHP includes the view in its view class so the declared variables aren't really in the global scope and global has no effect.
From ADmad (source):
When you run the file by itself your assignment $testVar = 'Hello
World!' is in global context hence things work like you expected them
to. But when it is used as a view file, the file is included within a
View class function hence its no longer in global context and $testVar
is no longer a global var, hence your expectation is incorrect. Using
global variables in an OOP framework is a bad idea anyway.

Related

Can I still use global $var inside a function in a laravel view?

I am a beginner in Laravel and I am trying to move an existing Project (in native php) to the laravel framework.
Everything so far is working great, except the part where I am trying to execute static functions from my helper classes.
I got a view called old.php, which looks like this:
$my_global_var = 'Hello';
class TestClass {
public static function foo(){
global $my_global_var;
return $my_global_var;
}
}
dd(TestClass::foo());
And instead of returning "Hello" like it used to, the function returns null instead.
I know I could just pass $my_global_var as a parameter instead, but as I mentioned before, I am trying to move an existing project to laravel. And that project is full of helper classes like TestClass, that are included at the beginning (or autoloaded) and then used throughout the entire programm.
My Question is: Do I have to rewrite all of my helper classes, or is there a way for me to keep the global $my_global_var; line?
global $my_global_var; // Here is your answer
$my_global_var = 'Hello';
class TestClass {
public static function foo(){
global $my_global_var;
return $my_global_var;
}
}
dd(TestClass::foo());
In you example, you can access the $my_global_var in your function using $GLOBALS[]. More info here.
Example:
$my_global_var = 'Hello';
class TestClass {
public static function foo(){
return $GLOBALS['my_global_var'];
}
}
dd(TestClass::foo());

Call a non-global variable from a function?

During the development of a Joomla! plugin, I ran across something really interesting. One of the events does not have a return value, but it calls variables from inside the function. Prior knowledge tells me this should only work if the variables are global inside the function, yet the dispatcher is able to call the variables from outside the function.
EDIT: I just discovered that the variable that is accessed from inside the function needs to be one of the paramaters! Could this be func_get_params() or call_user_func()?
Calling Code:
$instance = JDispatcher::getInstance();
$instance->trigger(onJoomCalledEvent, array(&$link, $other_params));
Plugin (snippet):
class plgMyPlugin extends JPlugin{
onJoomCalledEvent($link, $other_params){
$link = "Some Value Here";
return false;
}
}
This function returns false, yet somehow the application (Joomla!) is able to extract the value of $link. How is this done?
Does the plug-in definition look like this:
class plgMyPlugin extends JPlugin{
onJoomCalledEvent(&$link, $other_params){
$link = "Some Value Here";
return false;
}
}
Than it's pass by reference. If it's indeed the way you posted above than it's call time pass by reference which is deprecated and emits a warning starting with PHP 5.3.

Kohana globals being cleared?

I wrote a question earlier trying to get around using a controller in Kohana, but I gave in and rewrote the file. So now, the I have this php script inside a Kohana controller. The view that the controller is rendering is 'requiring' a php file so I can perform some third-party functions. I was having a major issues with it and got to debugging. The problem seems that functions inside the included file cannot access variables outside the function from within that same file. It seems that Kohana is clearing the globals somehow???
example:
//controller.php
require_once("ccfunctions.php");
//ccfunctions.php
$test = 'something';
function test(){
global $test;
echo $test;
}
test();
//This does not produce anything
Any thoughts on this one?
EDIT:
Actually, the above example doesn't work even from my view file that is being rendered. Forget the included file. I realize Kohana tries to enforce the MVC model, but this is ridiculous. Why cannot I not create a function and call a global variable from within my view file?
You need to specify global for variable in both cases:
//controller.php
require_once("ccfunctions.php");
//ccfunctions.php
global $test;
$test = 'something';
function test(){
global $test;
echo $test;
}
test();
//This does not produce anything
Btw, it is really weird practice and I believe there are workarounds for any case without using global
Don't use globals. Why use an OOP framework when you want to use globals?

Unable to access global variable in included file

I am having an unexpected issue with scope. The include documentation (also applies to require_once) says the required file should have access to all variable at the line it was required.
For some reason I am not able to access a class instantiated with global scope inside a function that was required in.
Would anyone know why? I am obviously missing something.
I got it working through a reference to $GLOBALS[], but I still want to know why it is not working.
UPDATE:
The error I am getting is:
Fatal error: Call to a member function isAdmin() on a non-object in <path>.php on <line>
Code:
$newClass = new myClass();
require_once("path to my file");
----- inside required file -----
function someFunction() {
$newClass->someMethod(); // gives fatal error. (see above).
}
Functions define a new scope, so inside a function you cannot access variables in the global scope.
Variable Scope
within user-defined functions a local
function scope is introduced. Any
variable used inside a function is by
default limited to the local function
scope
About included files, the manual states:
When a file is included, the code it
contains inherits the variable scope
of the line on which the include
occurs.
So if you include something in a function, the included file's scope will be that of the function's.
UPDATE: Looking at your code example edited into the question, global $newClass; as the first line of the function should make it working.
$newClass = new myClass();
require_once("path to my file");
----- inside required file -----
function someFunction() {
global $newClass;
$newClass->someMethod();
}
Be aware though that using global can quickly make your code more difficult to maintain. Don't rely on the global scope, you can pass the object to the function as a parameter, or use a Singleton/Registry class (some tend to argue against the latter, but depending on the case it can be a cleaner solution).
The included code doesn't have a scope different than the code surrounding it. For example:
function a() {
echo $b;
}
This will fail even if echo $b is in an included file. If you replace the above with:
function a() {
include 'file.php';
}
... and file.php contains:
echo $b;
... then it's the same thing as if you wrote:
function a() {
echo $b;
}
Think of it this way: whenever you use include / require, the contents of the included file is going to replace the include / require statement, just as if you removed the statement and pasted the contents of the file in its place.
It doesn't do anything else as far as scope is concerned.

Variables loaded from a separate script are not defined within functions

I use settings.php to store general settings for my application. When I load this settings file, I can use the variables defined in settings.php in the script itself, but not within any functions I define in it.
For example, in my class definition, myclass.php:
<?php
$preIP = dirname(__FILE__);
require_once( "preIP/settings.php" );
class MyClass {
...
public function foo() {
echo $variable_from_settings;
}
}
The code in the function foo() will not work (the variable will not be defined).
The settings.php file looks like this:
$variable_from_settings = "bar";
Thanks,
How about putting
global $variable_from_settings
before
echo $variable_from_settings;
You could do the following if you don't want to put global $variable_from_settings; everywhere.
echo $GLOBALS['variable_from_settings'];
However it's probably better to use a singleton to contain your settings as suggested in create superglobal variables in php?

Categories