Viewed   73 times

I have the following (stripped down) code:

<?PHP
    class A {
        function Show(){
            echo "ciao";
        }
    }

    $a = new A();
    $b = new B();

    class B {
        function __construct() {
            $a->Show();
        }
    }
?>

With a bit of surprise I cannot access the globally defined $a variable from within the class and I get a Undefined variable exception. Any help?

 Answers

5

Why the surprise? That's a pretty logical variable scope problem there...

I suggest you use either the global keyword or the variable $GLOBALS to access your variable.

EDIT: So, in your case that will be:

global $a;
$a->Show();

or

$GLOBALS['a']->Show();

EDIT 2: And, since Vinko is right, I suggest you take a look at PHP's manual about variable scope.

Sunday, September 25, 2022
 
5

Here's what worked best for me when trying to script this (in case anyone else comes across this like I did):

$ pecl -d php_suffix=5.6 install <package>
$ pecl uninstall -r <package>

$ pecl -d php_suffix=7.0 install <package>
$ pecl uninstall -r <package>

$ pecl -d php_suffix=7.1 install <package>
$ pecl uninstall -r <package>

The -d php_suffix=<version> piece allows you to set config values at run time vs pre-setting them with pecl config-set. The uninstall -r bit does not actually uninstall it (from the docs):

vagrant@homestead:~$ pecl help uninstall
pecl uninstall [options] [channel/]<package> ...
Uninstalls one or more PEAR packages.  More than one package may be
specified at once.  Prefix with channel name to uninstall from a
channel not in your default channel (pecl.php.net)

Options:
  ...
  -r, --register-only
        do not remove files, only register the packages as not installed
  ...

The uninstall line is necessary otherwise installing it will remove any previously installed version, even if it was for a different PHP version (ex: Installing an extension for PHP 7.0 would remove the 5.6 version if the package was still registered as installed).

Monday, December 12, 2022
1

include global $myarray at the start of setvalue() function.

public function setvalue() {
    global $myarray;
    $myvalue = $myarray[0];
}

UPDATE:
As noted in the comments, this is bad practice and should be avoided.
A better solution would be this: https://.com/a/17094513/3407923.

Wednesday, September 7, 2022
 
4

Never used any of those, but they look interesting..

Take a look at Gearman as well.. more overhead in systems like these but you get other cool stuff :) Guess it depends on your needs ..

Friday, November 11, 2022
 
1

Yes, conceptually this is right. Only you are not calling the function, at least not before writing Number to the document.

Btw, Number is the global reference to the Number constructor so you should use another variable name, lowercase at best.

var num = 100;
function outcome() {
    num--;
}
outcome();
document.write(num); // 99

or

<script>
var num = 100;
function outcome() {
    num--;
    alert(num);
}
</script>
<button onclick="outcome()">Decrease!</button>

(Demo at jsfiddle.net)

Friday, November 25, 2022
 
ferdau
 
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :