php / php/doc-en

get_defined_vars() scope clarification

Open
#1,317 2 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
XML
Stars
596
Forks
890
Avg merge
1d 15h
Merged PRs (30d)
55

Description

The manual for get_defined_vars() reads:

This function returns a multidimensional array containing a list of all defined variables, be them environment, server or user-defined variables, within the scope that get_defined_vars() is called.

However, I notice that superglobals like $_GET, $_POST, $_SERVER etc. are within a function's scope (right?), however they are only returned when called outside any scoping context. Calling get_defined_vars() inside a function only returns user-defined variables, if any. Investigating further, a scoping test shows that:

- function no_vars: array(0) { }
- function has_vars: array(1) { ["var"]=> int(1) }
- function has_args: array(1) { ["arg"]=> int(1) }
- anon function: array(0) { }
- anon function with var: array(1) { ["var"]=> int(1) }
- anon function with use(): array(1) { ["used"]=> int(1) }
- blank class: array(0) { }
- class with properties: array(0) { }
- static class with properties: array(0) { }
- no context: array(10) {
	 ["_GET"]=> array(0) { }
	 ["_POST"]=> array(0) { }
	 ["_COOKIE"]=> ...
	 ["_FILES"]=> array(0) { }
	 ["func"]=> object(Closure)#1 (0) { }
	 ["funcVar"]=> object(Closure)#2 (0) { }
	 ["used"]=> int(1)
	 ["funcUses"]=> object(Closure)#3 (1) { ["static"]=> array(1) { ["used"]=> int(1) } }
	 ["cBlank"]=> object(classBlank)#4 (0) { }
	 ["cProps"]=> object(classProps)#5 (3) { ["foo"]=> int(1) ["bar":protected]=> int(2) ["meh":"classProps":private]=> int(3) }
}

In keeping with the "returns variables explicitly available in a given scope" (and excluding superglobals) behavior, the results are mostly as expected. However, I find the following a bit anomalous:

  • when called inside a class constructor, properties are not returned -- even though they are within scope
  • when called in global scope, the properties of an object instance are however returned
  • in global scope, superglobals $_SERVER and $_ENV (PHP 5.4+) are not returned

I think these behaviors should be mentioned explicitly in the manual, along the lines of:

  • In global scope, a list of all defined variables, whether superglobals, command line arguments or user-defined variables
  • In function scope, only a list of user-defined variables (both arguments and in-body definitions)
  • In class/object method scope, does not return class/object properties;

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the get_defined_vars() manual page linked in the issue and verify the reported behavior for global, function, and class/object scopes. Update the scope description to explain which variables are returned, including the noted superglobal and property differences, and confirm the examples match the documented behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
php
Domain
documentation
Issue type
Documentation
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
55/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.