Practical PHP Class OOP Tutorial

Practical PHP Class OOP Tutorial

Listening Video

This is a practical class file creation and usage tutorial. Working with class files and objects opens up a wider range of modularity and helps you avoid instance collisions when many object instances simultaneously access a method or property. Modularity - refers to the compartmentalization and inter-relation of the parts of a software package. Object oriented programming should only be used when you have a practical need to reuse a scripted module, or share a complex scripted module with others in a common usage format. You can give people a few simple usage instructions and your class file to allow them to have all the programming power you provide in the class package. strongpass.php(the class file)
<?php class strongpass { public function check($password){ $response = "OK"; if(strlen($password) < 8){ $response = "Password must be at least 8 characters"; } else if(is_numeric($password)){ $response = "Password must contain at least one letter"; } else if(!preg_match('#[0-9]#', $password)){ $response = "Password must contain at least one number"; } /* Additional checks you can accomplish as homework - Make sure there is at least one lowercase letter - Make sure there is at least one uppercase letter - Make sure there is at least one symbol character */ return $response; } } ?>
demo.php(file that calls the strongpass class into action)
<?php $password = ""; $status = ""; if(isset($_POST["password"])){ $password = $_POST["password"]; include_once("strongpass.php"); $strongpass = new strongpass(); $response = $strongpass->check($password); if($response != "OK"){ $status = $response; } else { $status = "Password is strong so parsing can continue here."; } } ?> <h2>Class strongpass Demo</h2> <form action="demo.php" method="post"> <input name="password" type="password" value="<?php echo $password; ?>"> <input type="submit" value="Check Password Strength"> </form> <h3><?php echo $status; ?></h3>

0 Response to "Practical PHP Class OOP Tutorial"

Post a Comment