Call a non-global variable from a function? - php

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.

Related

making a class's static-member a function (closure) does not work

(PHP7)
Consider the following code, which tries to assign a function to a variable, and then make sure it is called only once.
class a{
static public $b;
static public function init(){
self::$b();
self::$b=function(){};
}
}
a::$b=function(){echo 'Here I do very heavy stuff, but will happen only in the first time I call init()';};
for($i=0;$i<1000;$i++){
a::init();
}
In php7 it will give an error that it expects a::$b to be a string (the function name to call).
If I use pure variables and not static members, it will work.
My question, is this suppose to work, or not, or is there a small tweak I can do for this to work without pure vars?
You can either use PHP 7 Uniform Variable Syntax:
(self::$b)();
Or a temporary variable in PHP 5+ (including 7):
$init = self::$b;
$init();
As seen on 3v4l.org.

PHP: class variable visibility

I'm just a once-in-a-while PHP developer. Now, working on a legacy application, I just hit upon the following problem, which seems very stupid. But I can't get $someString class variable to hold the right value:
class MyClass{
var $someString;
function doSomething(){
//$this->setString(); //this is effectively not called here, but later in getIframe()
$this->buildIframe();
echo $this->someString; //actually, I need someString here, but it is empty
}
function setString(){
$this->someString = "something";
}
function buildIframe(){
$content .= <iframe....>;
}
function getIframe(){
$this->setString();
}
}
$myClassInstance = new MyClass();
$myClassInstance->doSomething();
$myClassInstance->getIframe();
As far as I can see, doSomething() is called in a class context, as I did show.
What am I doing wrong?
EDIT:
I reviewed the code and I think I found whats causing this. There is an iframe embedded into the html output, which is generated at one part and called later on. So the setString() method is actually not called imediately, what I thought first but when invoking the iframe code. So thats why it's not available where I need the string output.
I guess like the code is now, there is no way to get the $someString output except inside the getIframe() method.
This code is 100% correct and working. I've checked it on PHP 5. It echos "something" in string. And it is proper behavior.
From manual:
Note: The PHP 4 method of declaring a variable with the var keyword is still supported for compatibility reasons (as a synonym for the public keyword). In PHP 5 before 5.1.3, its usage would generate an E_STRICT warning.

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.

Store and use global variables in the same class, but different public functions using php

I have a project made in netbeans with php and I am using Zend framework, so it's model-view-controller.
Into a class (newclassController), I have 4 public functions. I want to store a global variable from the second function and use it into the 4-th function.
I have tried to send the variable in the URL as a parameter and SESSION variables but I get some errors so if you have some other ideeas please enlight me.
All I tried returns gives this error message:
Notice: Undefined variable
NOTE: It works if I store the variable in the init function, but I want to store it from another function.
unless I miss my guess when you initiate a value in:
public function init(){
$this->value = 'value';
}
the value is available to all of the 'actions' in the controller.
This is by design.
The init() method is used in ZF1 to supplement the constructor. It's where you add arguments that might normally be put in the constructor.
In order to persist a value from one action to another, some form of storage must be used.
For example:
//a verbose example
public function indexAction(){
//initiate a variable
$i = 100;
//start a new session and assign a name to it
$session = new Zend_Session_Namespace('number');
//assign the value to the namespace
$session->value = $i
}
later in that same controller or even another controller
public function newAction(){
//access the session
$session = new Zend_Session_Namespace('number');
//assign the value to the view
$this->view->value = $session->value;
}
now in the new.phtml view
<?php echo $this-value ?>
An important thing to remember when using PHP and specifically Zend Framework 1, every request runs the entire application.
This was a concept that tripped me up in the beginning. Every time you request a new page your Zend Framework application runs from scratch, so any data that needs to survive from one request to the next must be saved (persisted).
public function__constructor(){
$this->value = 'something';
}
$this->value
will give 'something' in the class
decalre the variable in the constructor of the class and use it

PHP create_function Instance variable - Unable to call anonymous function: Follow up

This is somewhat a follow up to a previous question - but I've distilled the question down and have the "works" vs. "doesn't work" cases narrowed down much more precisely.
My Goal:
I have a class MyClass that has an instance variable myFunction. Upon creating a MyClass object (instantiating), the constructor assigns the instance variable myFunction with the result of a call to create_function (where the code and args come from a db call).
Once this object of type MyClass is created (and stored as an instance variable of another class elsewhere) I want to be able to call myFunction (the instance variable anonymous function) from "anywhere" that I have the MyClass object.
Experimental Cases -- below is my highly simplified test code to illustrate what works vs. what doesn't (i.e. when the expected functionality breaks)
class MyClass extends AnotherClass {
public $myFunction;
function __construct() {
$functionCode = 'echo "NyanNyanNyan";';
$this->myFunction();
/*Now the following code works as expected if put in here for testing*/
$anonFunc = $this->myFunction;
$anonFunc(); //This call works just fine (echos to page)!
/*And if i make this call, it works too! */
self::TestCallAnon();
}
public function TestCallAnon() {
$anonFunc2 = $this->myFunction;
$anonFunc2();
}
}
However, if I do the following (in another file, it errors saying undefined function () in... within the Apache error log.
//I'm using Yii framework, and this is getting the user
//objects instance variable 'myClass'.
$object = Yii::app()->user->myClass;
$object->TestCallAnon(); // **FAILS**
or
$func = $object->myFunction;
$func(); // ** ALSO FAILS **
In addition, several variations of calls to call_user_func and call_user_func_array don't work.
If anyone is able to offer any insight or help that would be great :).
Thanks in advance!
You can't pass references to functions around in PHP like you can in for instance JavaScript.
call_user_func has limited functionality. You can use it like so:
class MyClass {
function func() {}
static function func() {}
}
function myfunc() {}
$i = new MyClass();
call_user_func("myfunc", $args);
call_user_func(array($i, "func"), $args);
call_user_func(array(MyClass, "staticFunc"), $args);
I ended up solving this issue via a workaround that ended up being a better choice anyways.
In the end I ended up having a static class that had a method to randomly return one of the possible identifiers, and then another method which accepted that identifier to build the anonymous function upon each class.
Slightly less elegant than I would like but it ends up working well.
Thanks to everyone for your efforts.

Categories