Bug #80941 [Com]: function __construct() don't pass $var to context GLOBAL

From: Date: Thu, 08 Apr 2021 16:02:47 +0000
Subject: Bug #80941 [Com]: function __construct() don't pass $var to context GLOBAL
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-233319@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=80941&edit=1

 ID:                 80941
 Comment by:         rtrtrtrtrt at dfdfdfdf dot dfd
 Reported by:        rg at mejoramos dot com
 Summary:            function __construct() don't pass $var to context
                     GLOBAL
 Status:             Not a bug
 Type:               Bug
 Package:            Output Control
 Operating System:   any
 PHP Version:        8.0.3
 Block user comment: N
 Private report:     N

 New Comment:

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


Previous Comments:
------------------------------------------------------------------------
[2021-04-08 15:48:16] 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 15:12:50] 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:00:19] 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


------------------------------------------------------------------------



--
Edit this bug report at https://bugs.php.net/bug.php?id=80941&edit=1


Thread (5 messages)

« previous php.bugs (#233319) next »