简体   繁体   中英

Error defining variable in PHP object

I am getting the following error in the following code:

Class primeField implements field {
    private $intmodulus = '';
    public function generator(){
        return ;
    }

    public function modulus(){
        return $this->$intmodulus;
    }
    public function __construct($modulus , $base=0) {
        if (is_resource($modulus) && get_resource_type($modulus) == "GMP integer"){
            $this->$intmodulus = $modulus;
        } else{
            $this->$intmodulus = gmp_init($modulus , $base); \\line 70
        }
    }
}
$a = new primeField(11);
$a->modulus();

Notice: Undefined variable: intmodulus in /Users/admin/PHP ECC/finitefield.php on line 70 Fatal error: Cannot access empty property in /Users/admin/PHP ECC/finitefield.php on line 70

Why

The syntax is

$this->intmodulus 

not $this->$intmodulus .

You get an error saying "cannot access empty property" because $intmodulus is undefined and hence accessing it gives NULL . The NULL gets converted into an empty string when you attempt to use it as a property name.

If the value of $intmodulus was the name of a valid property (eg if $intmodulus == "intmodulus" ), you would be accessing the property with that name.

$this->$intmodulus should be $this->intmodulus

See the PHP documentation for Variable variables for info on what is happening.

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