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.

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 a read? Commented Oct 25, 2019 at 15:05
  • have you tried "$secretcode" (with")? Commented Oct 25, 2019 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. Commented Oct 25, 2019 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 Commented Oct 25, 2019 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 Commented Oct 25, 2019 at 15:09

1 Answer 1

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; 
    } 
}
Sign up to request clarification or add additional context in comments.

5 Comments

Problem is that you are using the constructor to set a static property, you may never create certain types of object depending on how they are used so the secret may never be set. If the objects are created, it will set a new secret each time - overwriting the previous one.
@NigelRen updated the solution.
Still doesn't cater for it never being set! Also it now doesn't set the value at all.
@NigelRen looking forward to see your answer...:)
Very unlikely as I closed this question as a duplicate 15 minutes ago.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.