Bug #80941 [Com]: function __construct() don't pass $var to context GLOBAL
Edit report at https://bugs.php.net/bug.php?id=80941&edit=1
ID: 80941
Comment by: rg at mejoramos dot com
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:
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();
}
Previous Comments:
------------------------------------------------------------------------
[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)