Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8037739
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T03:00:46+00:00 2026-06-05T03:00:46+00:00

is any way to return reference at class parameter or global variable? I try

  • 0

is any way to return reference at class parameter or global variable? I try this

class test{

  public static $var;

  public static function get(&$ref){

    $ref = self::$var;

  }

}

test::get($ref);
$ref = 'test';
var_dump(test::$var);

it’s a basic example, i know, then this example can be use another way, but i need to keep principle


this is my function, where is problem with reference to variable

class mySession{

    public static function issetKeys(){

      $keys = func_get_args();

      $session = &$_SESSION;
      $c = 0;

      if (is_array($keys)){
        foreach ($keys as $val){

          if (isset($session[$val])){
            $session = &$session[$val];
            $c++;
          }
          else break;

        }
      }

      return $c == count($keys);

    }

    public static function &get(){

      $keys = func_get_args();

      $session = &$_SESSION;

      if (is_array($keys)){
        foreach ($keys as $val){

          if (!isset($session[$val])) $session[$val] = Array();
          $session = &$session[$val];

        }
      }

      return $session;

    }

  }

  function getValue(){

    if (!mySession::issetKeys('p1', 'p2')){
      $session = mySession::get('p1', 'p2');
      $session = 'string';
    }

    return mySession::get('p1', 'p2');

  }

  print_r($_SESSION);

but no variable save in to $_SESSION

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T03:00:47+00:00Added an answer on June 5, 2026 at 3:00 am

    In the general case, a parameter only exists for the duration of the function to which it was passed; when the function returns, the call stack is unwound and the parameter disappears, which means that any reference to a variable that lived in the call stack will no longer be valid (since it points to memory which has been freed and now quite possibly contains something else entirely.)

    That said, it is possible to return a reference to a parameter, although this will not work if you try to return a reference to a variable that actually lives in the stack (that is, one that was not passed by reference):

    $myGlobal = 10;
    
    // Note the & before the function name
    function &returnReference(&$x) {
        return $x;
    }
    
    // Note the & before the function call
    $someValue = &derp($myGlobal);
    echo $myGlobal;
    
    $someValue = 0;
    echo $myGlobal;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there any way to return a readonly instance of an object? public class
Is there any way to return an array from a function? More specifically, I've
Is there any way to have a function in PostgreSQL return an arbitrary type?
Hey guys I was wondering if there was any way to return a certain
I would like to know if there is any way to return an char
Is there any way to catch key combination ctrl + x + return in
Is there any way to do the following: validateLogin(); return false; But actually like
Any way of using the data_response outside the $.post() ? This is part of
Is there any way to return an STL iterator to a std::map (e.g. std::map<const
Is there any way to get the reference of Date Picker Dialog from Date

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.