简体   繁体   中英

How to put a property class as a variable in php?

So hi, I'm actually a very very beginner and i'd like to know how to set a property that is in a class as a variable to do condition?

class Utilisateur
{
public $mdp, $nom;
   function mot($mdp)
   {
     if (strlen($mdp)<=8) 
     {
      echo "Votre mot de passe n'est pas fort. ";
     }
   }
}

So the property i want to set as a variable is $mdp, but i'm not sure if i can really do this. Thanks for your future replies.

What you might typically do would be to assign the variables as parameters to the class constructor and define them as properties within the class - allowing access afterwards using $this->var style syntax

class Utilisateur{
    public $mdp;
    public $nom;
    
    public function __construct($mdp=false,$nom=false){
        $this->mdp=$mdp;
        $this->nom=$nom;
    }

    function mot(){
        if ( strlen( $this->mdp ) <= 8 ) {
            echo "Votre mot de passe n'est pas fort.";
        }
    }
}

$obj=new Utilisateur('banana','fred');
$obj->mot();

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