简体   繁体   中英

__construct function not working as expected

I'm trying to build a __construct function for my class. This function should get All $_REQUEST values and store them in an array so they can be called later by id. The problem is that it doesn't store my variables and I don't know why.

Seeing this is my first "serious" attempt at a construct, it's most probably my stupidity. But I'd like to know what that is.

Class Regex {

private static $requests = array();

    function __construct() {
        foreach($_REQUEST as $key => $value) {
            self::$requests[$key] = array(
                'value' => $value,
                'status' => false,
                'errorList' => array()
            );
        }
    }



    public static function preg($key, $rules) {
        var_dump(self::$requests); // for test purpose
    }
}

The result of above is: array (size=0) empty .

Are you even calling the constructor? A constructor is only called when calling it either explicitly or via the new keyword).

PHP doesn't have anything like static constructors like Java has.

You have to ensure that the array is filled at the first access to preg() method:

public static function preg($key, $rules) {
    if (empty(self::$requests)) {
        foreach($_REQUEST as $key => $value) {
            self::$requests[$key] = array(
                'value' => $value,
                'status' => false,
                'errorList' => array()
            );
         }
    }
    var_dump(self::$requests); // for test purpose
}

the constructor of your Regex class is called upon creating a new regex object like so:

$regex = new Regex;

but you never create a Regex object so the constructor is never called, resulting in an empty $requests array.

You work with a static function. I think you don't call the construct method. The __construct function is called if you make a new instance.

$regex = new Regex;

If you call the static class for example Regex::preg the contructor is not called.

Expanding upon what bwoebi has answered, you can still get the intended results simply by adding a call to the static function preg from the constructor itself as you can see bellow:

That said, this is just adding more bloat without any real benefit.

Whilst this could come in useful in certain cases, and I only added it within this answer as a mean to display that your current structure could work with the simple addition of two simple lines, I would recommend going with @bwoebi's answer but to keep in mind that whilst the controller is initself not static, it does not in any way or form stop it from communicating with static methods.

PHP:

class Regex {

    private static $requests = array();

    function __construct(){
        if (empty(self::$requests)) {
            foreach($_REQUEST as $key => $value) {
                self::$requests[$key] = array(
                    'value' => $value,
                    'status' => false,
                    'errorList' => array()
                );
            }
        }
        self::preg();
    }

    public static function preg(){
        var_dump(self::$requests); // for test purpose
    }
}

$var = new Regex();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM