--- Restore content start ---
Overview:
To better understand the object-oriented programming thinking, we recommend several common tips to quickly improve object-oriented programming.
1. Farewell To Constants
2. Farewell to variables
3. Farewell to static variables
4. Farewell to Functions
5. Farewell to global variables
6. Farewell to map Array
Other articles
Articles of the same kind: Object-Oriented Knowledge-new knowledge, object-oriented thinking-sleepwalking (1), object-oriented understanding-how to find a class
Object-oriented practice: Teach you how to create a keyword matching project (search engine) ---- the first day, teach you how to do a keyword matching project (search engine) ---- 21st days (new)
Server Load balancer configuration: Server Load balancer-concept understanding, Server Load balancer-configuration implementation (nginx), Server Load balancer-File Service Policy
Tips
1. constant conversion to constant type
Constant instance:
define("LEVEL_ERROR",‘error‘);define("LEVEL_WARNING",‘warning‘);define("LEVEL_INFO",‘info‘);define("LEVEL_TRACE",‘trace‘);
Common instance:
class Level { const ERROR = ‘error‘; const WARNING = ‘warning‘; const INFO = ‘info‘; const TRACE = ‘trace‘;}
2. convert a variable to an attribute
Variable instance:
$ Username = "Zhang San"; $ userage = 18; $ useraddress = "XX Province, XX city, xx Town, XX Village ";
Attribute instance:
class User { private $name; private $age; private $address; /** * @param mixed $address */ public function setAddress($address) { $this->address = $address; } /** * @return mixed */ public function getAddress() { return $this->address; } /** * @param mixed $age */ public function setAge($age) { $this->age = $age; } /** * @return mixed */ public function getAge() { return $this->age; } /** * @param mixed $name */ public function setName($name) { $this->name = $name; } /** * @return mixed */ public function getName() { return $this->name; } public function __construct($name="",$age=0,$address=""){ $this->name = $name; $this->age = $age; $this->address = $address; }}
$ User = new user ("Zhang San", 18, "XX Province, XX city, xx Town, XX Village ");
3. Convert static variables to static attributes
Static variable instance:
static $app;if($app == null){ $app = new App();}
Static attribute instance:
class App { private static $app = null; public function instance(){ if(self::$app == null){ self::$app = new self; } return self::$app; }}
4. Convert static functions to static functions
Static function instances:
function version(){ return "1.0.0.0";}
Static Method Instance:
class App { public static function version(){ return "1.0.0.0"; }}
5. convert global variables to attributes
Global variable instance:
function login($password){ global $user; if($user->password === $password){ return true; } throw new Exception("invalid password!");}
Attribute instance:
class UserService { private $user; /** * @param mixed $user */ public function setUser($user) { $this->user = $user; } /** * @return mixed */ public function getUser() { return $this->user; } public function login($password){ if($this->getUser()->password == $password){ return true; } throw new Exception("invalid password!"); }}
6. Map array to object
Map array instance:
$orderItems = array();function addItem($product,$num,&$orderItems){ $orderItems[] = array("product"=>$product,"num"=>$num);}
Object instance:
class Order { private $items; public function addItem($product,$num){ $this->items[] = new OrderItem($product,$num); }}class OrderItem { private $product; private $num; public function __construct($product,$num){ $this->product = $product; $this->num = $num; }}
Summary
These are some common skills that can be mastered by them. It may be a pleasure for you in your future programming career.
Object-Oriented Knowledge-class conversion