开发者

PHP global declaration

开发者 https://www.devze.com 2023-01-06 10:54 出处:网络
I\'m using PHP\'s global declaration to make an array available to a number of functions in a script.The variable is declared at the top of the script and is referenced with global in each of the func

I'm using PHP's global declaration to make an array available to a number of functions in a script. The variable is declared at the top of the script and is referenced with global in each of the functions which uses it, as follows:

<?php

$myarray = array(1, 2, 3);

function print_my_array() {
    global $myarray;

    print '<ul>';
    foreach($myarray as $entry) {
        print '<li>'.$entry.'</li>';
    }
    print '</ul>';  

    return 0;
}

print_my_array();

?>

Sometimes, but not a开发者_开发知识库lways, the array is not set when the function is called, generating an error when the foreach is called. In the actual code, the array used is given a very unique name and so should not be causing any collisions with anything else. Am I mis-using the global declaration?


No, the snippet is correct. The problem you're having is the problem of using global variables – they can be accessed and changed from anywhere (perhaps accidental), thereby creating hard-to-find bugs.


By using globals you can hit quite a few gotchas, they'll also make you code less reusable.

Here's an example of your function which can be re-used many times across the site.

(untested)

<?php
function arrayTags($items, $open = '<li>', $close = '</li>')
{
    if (is_array($items) && count($items) != 0)
    {
        $output =   null;

        foreach ($items as $item) {
            $output .= $open . $item . $close;
        }

        return $output;
    }
    else
    {
        return '';
    }
}

// Default, <li>
echo '<ul>' . arrayTags($myarray) . '</ul>';

// or, spans:
echo '<div id="container">' . arrayTags($myarray, '<span>', '</span>') . '</div>';


The least you could do is check if the array is null at the top of the function, before you run the foreach. that would at least prevent the error:

function print_my_array() {
    global $myarray;

    if(!empty($myarray)) {
        print '<ul>';
        foreach($myarray as $entry) {
            print '<li>'.$entry.'</li>';
        }
        print '</ul>';  
    }
}

Also, I wouldn't just return 0 for the hell of it. You may want to incorporate whether or not the array was empty into what you return from this function.


$myarray = array(1, 2, 3);

In short you have to only declare it like so:

$myarray = array(); 

and if you want to populate it with values do that in the class constructor:

public function __construct(){ 
    $myarray = array(1,2,3);  
}

I'm no guru, but in my experience it seems that php doesn't like to execute function calls outside of a function within a class.

THIS DOES NOT WORK:

class MyClass {
    public $mystring = myfunction();

    public function myFunction(){
        return true; //and your function code
    }
}

so when you use array() it doesn't actually trigger any function call, it just creats an empty variable of type array. when you use array(1,2,3), it has to effectively run the 'create array' which is like a function.

I know annoying, I'd like it to be different, but I don't know a way of doing what you want in php. Let me know if there is a nice way I'd love to hear it!

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号