How to “globalize” PHP variables?

流过昼夜 提交于 2019-12-25 18:03:31

问题


I have a page named ChangeApprovalInfo.php - It has a function called Row_Rendered as follows;

function Row_Rendered() {

    // To view properties of field class, use:
    //var_dump($this-><FieldName>);

    $RecordOwner = $this->RequestUser->CurrentValue;  
        echo $RecordOwner;
} 

Echoing $RecordOwner gets me the data I will need for a sql query on another page....

I have another page called ChangeApprovalEdit.php - This page has

<?php include_once "ChangeApprovalinfo.php" ?>

at the top of the file.

ChangeApprovalEdit.php has a function where I need the $RecordOwner variable as defined in ChangedApprovalInfo.php

If I add "echo $RecordOwner" on the ChangeApprovalEdit.php page, I get an error saying it's an unknown variable. My understanding is that I need to "make it global" or some such business. I know very little about PHP and the pages I am editing are long and complex. (to me, at least)

  • What do I need to do? I know that the information I have provided might not be enough to answer the question. I don't know enough to even know exactly what I need to ask. If more information is needed, I will edit and follow up.

pastebin of the files

ChangeApprovalInfo.php = http://pastebin.com/bSRM1wwN
ChangeApprovalEdit.php = http://pastebin.com/AStG9pqb

EDIT: Changing Row_Rendered to this seems to be more effective. I'm having trouble seeing WHERE I can later echo this variable... but I'm getting somewhere with this...

function Row_Rendered() {
    // To view properties of field class, use:
    //var_dump($this-><FieldName>);
    $GLOBALS['RecordOwner'] = $this->RequestUser->CurrentValue;  
} 

回答1:


Don't echo variables from functions, which just outputs them to the standard output. return them from the function so you can use the value elsewhere as well.

function Row_Rendered() {
    $RecordOwner = $this->RequestUser->CurrentValue;  
    return $RecordOwner;
} 

Then instead of

$obj->Row_Rendered();

use

echo $obj->Row_Rendered();

and if you want to use the value elsewhere, use

$value = $obj->Row_Rendered();



回答2:


You can do a couple of things:

First, you can return $RecordOwner from the function, and store its value in a variable. This method is usually preferred.

function Row_Rendered() {

    // To view properties of field class, use:
    //var_dump($this-><FieldName>);

    $RecordOwner = $this->RequestUser->CurrentValue;  
        echo $RecordOwner;

    return $RecordOwner;
} 

// Store it in a variable when calling the function.
$RecordOwner = Row_Rendered();

Or, you can make it global inside the function:

function Row_Rendered() {

    // To view properties of field class, use:
    //var_dump($this-><FieldName>);

    $GLOBALS['RecordOwner'] = $this->RequestUser->CurrentValue;  
        echo $GLOBALS['RecordOwner'];
} 



回答3:


You can use the $GLOBALS superglobals array, like this:

function Row_Rendered() {
  $GLOBALS['RecordOwner'] = $this->RequestUser->CurrentValue;  
}

However, you should not do that. Instead, refactor your application so that the view in ChangeApprovalinfo.php just contains a function, which is then called with the appropriate parameters.




回答4:


EDIT: Chaning Row_Rendered to this seems to be more effective. I'm having trouble seeing WHERE I can later echo this variable... but I'm getting somewhere with this...

function Row_Rendered() {
    // To view properties of field class, use:
    //var_dump($this-><FieldName>);
    $GLOBALS['RecordOwner'] = $this->RequestUser->CurrentValue;  
}

I feel compelled to write another answer to this update. Let me demonstrate the use of globals as seen from outside that function:

$obj->Row_Rendered();
$obj->foobar();

echo $GLOBALS['RecordOwner'];

Quick, what will be echoed and where does that value come from? Well, it depends on what $obj-foobar() does. Maybe it changes the global variable. Maybe it doesn't. Who knows if the variable has been set at all? How would you trace back what happened exactly without adding a debug line after every single function call?

And that's just three lines of code. Imagine that in an application of any complexity.

Now, the same thing if I return the value from Row_Rendered:

$owner = $obj->Row_Rendered();
$obj->foobar();

echo $owner;

If the Row_Rendered method is behaving as it should (returning the owner), this code is very predictable. If you do not follow this pattern, you'll have a hell of a time getting anything done when the application grows to any halfway complex size.




回答5:


Set the variable as global from within the function

$my_global_var = "old value";

function doing_stuff(){
  global $my_global_var; //this will use the global variable instead of creating a local one
  $my_global_var = "new value";
}

echo $my_global_var;//returns "new value"


来源:https://stackoverflow.com/questions/8845447/how-to-globalize-php-variables

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!