php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #80941 function __construct() don't pass $var to context GLOBAL
Submitted: 2021-04-08 15:00 UTC Modified: 2021-04-08 15:12 UTC
From: rg at mejoramos dot com Assigned:
Status: Not a bug Package: Output Control
PHP Version: 8.0.3 OS: any
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: rg at mejoramos dot com
New email:
PHP Version: OS:

 

 [2021-04-08 15:00 UTC] rg at mejoramos dot com
Description:
------------
no't possible pass $VAR to GLOBAL context without used ARRAY $GLOBALS, please see the code.

Test script:
---------------
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);

function OtherFunction(){
	GLOBAL $ManyFunctionsNeedThisVar;
	print_r($GLOBALS['a']);
	print_r($ManyFunctionsNeedThisVar);
	}

class OtherClass{
	function __construct(){
		$GLOBALS['a'] = 888;
		$ManyFunctionsNeedThisVar = 777;
		OtherFunction();
		}
	}

$OtherObject = new OtherClass();



Expected result:
----------------
String concatenated, correct output:

888777

Actual result:
--------------
888

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2021-04-08 15:12 UTC] dharman@php.net
-Status: Open +Status: Not a bug
 [2021-04-08 15:12 UTC] dharman@php.net
This looks correct to me. `$ManyFunctionsNeedThisVar` is empty in global scope. I think your confusion comes from the fact that you have another variable with the same name in a local scope within the constructor. Local scope and global scope are not the same.
 [2021-04-08 15:48 UTC] rg at mejoramos dot com
Thanks master @dharman

when we declare 

$ManyFunctionsNeedThisVar = 777;

this is GLOBAL.

When we declare

	function __construct(){ // --> this is GLOBAL ?
		$GLOBALS['a'] = 888; // --> this is GLOBAL ?
		$ManyFunctionsNeedThisVar = 777; // --> this is GLOBAL ?
		OtherFunction();
		}
 [2021-04-08 16:02 UTC] rtrtrtrtrt at dfdfdfdf dot dfd
why should $ManyFunctionsNeedThisVar in __constrcut(9 be global without "global $ManyFunctionsNeedThisVar" ir $GLOBALS['ManyFunctionsNeedThisVar'];

this are two different vars in different scopes and you really should learn basics:
https://www.php.net/manual/en/language.variables.scope.php
 [2021-04-08 16:12 UTC] rg at mejoramos dot com
Sorry by my ERROR.

Many thanks by your lesson of Aphril masters

@dharman and @rtrtrtrtrt

I am wrong about this,
I am sorry and thanks by your time.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Dec 27 06:01:29 2024 UTC