0
$n=800; 
function getName($n) { 
    $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; 
    $randomString = ''; 

    for ($i = 0; $i < $n; $i++) { 
        $index = rand(0, strlen($characters) - 1); 
        $randomString .= $characters[$index]; 
    } 

    return $randomString; 
} 

$secretcode = getName($n);

class Mess {
    protected static $options = array(
        'secret' => '$secretcode',
        'signing_method' => 'sha512',
        'verify' => true,
    );
}

This is my PHP code as shown, i want the variable "secretcode" or the value in this variable to put in at the position in the class where is an function with an array. In this array at the secret element of the array should be the varibale value. I heard about global variable but i tried so much with it, everytime it gives me error specific what i change back but i dont understand why things not work. Honest im have not used php classed yet so i dont know if it diffent usage with varibale then normal.

Laze Ho
  • 65
  • 7
  • I see you're confused about globals and the general scope of how a variable operates in a function and class. May I suggest you give the PHP docs about [variable scoping](https://www.php.net/manual/en/language.variables.scope.php) a read? – IsThisJavascript Oct 25 '19 at 15:05
  • have you tried `"$secretcode"` (with`"`)? – Hasta Dhana Oct 25 '19 at 15:06
  • yes i tried with "" and this '' but it dont work it shows an error but i dont know in other arrays i use "" as well without prolems. – Laze Ho Oct 25 '19 at 15:07
  • Unfortunetly the topic has been closed which means we can't provide answers. Please give both the duplicates a read as `'$secretcode'` is a literal string whereas `"$secretcode"` is the same as `$secretcode` – IsThisJavascript Oct 25 '19 at 15:09
  • i saw the php docs and tried this examples on my code but it dont works, with gloabl in front of the variable static in front of the varibale and all shown examples as well – Laze Ho Oct 25 '19 at 15:09
  • The main problem is the scope of the variable, you would to set the value (as the property is static) create a static method which will set this value and then call this method with your random string. It may be easier to just use something from https://stackoverflow.com/questions/18830839/generating-cryptographically-secure-tokens to generate the string anyway. – Nigel Ren Oct 25 '19 at 15:12
  • Just added another [dupe](https://stackoverflow.com/questions/693691/how-to-initialize-static-variables) which should solve your problem. – Nigel Ren Oct 25 '19 at 15:34

1 Answers1

1

You can use this as inside the class

class Mess {
    protected static $options = array(
     'secret' => '',
     'signing_method' => 'sha512',
     'verify' => true,
    );
    function __construct($number){
      !empty(self::$options['secret']) ? self::$options['secret'] : this->getName($number);
    }
    function getName($n) { 
      $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; 
      $randomString = ''; 
      for ($i = 0; $i < $n; $i++) { 
        $index = rand(0, strlen($characters) - 1); 
        $randomString .= $characters[$index]; 
      } 
      return $randomString; 
    } 
}
Rakesh Jakhar
  • 6,380
  • 2
  • 11
  • 20