PHP Object-oriented
Class:
Abstract of a class of common things
For example:
Eg: car
commonality: the commonality of classes
Eg: Wheels, active
Object:
Class made out of
Eg: bicycles, scooters, cars, trucks,
The object is the specific product in order to use the function
1.php creates a new class and instantiates (instantiation refers to the creation of a new object with a Class):
Definition Method:class Name {}
Public : property modifier: Used to control access to properties/methods
instantiated object: Object name = new class name ();
<?php /* Defines a class classes class name { } class is made up of n properties and methods
Eg: Define a class: Computer */
Class Computer { //public: Property modifier: Used to control access to properties/methods public $CPU = ' i5 four cores '; Public $memory = ' 88G '; Public Function Surf () { echo ' internet '; } Public Function Play () { echo ' play movie '; } Public Function Program () { echo ' programming '; } } Instantiated object: Object name = new class name (); $c = new computer (); method to invoke Class: Object, method name $c->play (); Play the movie Echo ' <br/> '; Read the property name of the object property , [note that the dollar sign is not required before the property] Echo $c->cpu;//i5 four-core?>
2. Property modifiers and this
There are 3 common property modifiers:
Public (Common)
Protected: Protected
Private: Proprietary (inheritance inside speaking)
This
Which object calls me, I point to WHO
Object.defineproperty class Computer { //public: Property modifier: Used to control access rights for properties/Methods */ common property modifiers in 3: Public (Common) protected: protected (inheritance inside) private: Outside of the private/ ///class: Outside of the definition of the class {}/inside of the class : {} in the definition of the class The inside public $keyboard = ' keyboard ';//public: The outside of the class and inside the class can access public $mouse = ' mouse '; Private $CPU = ' 4 nuclear i5 '; Private: Only inside the class can access public function Showinfo () { //this: Which object calls me, I'll point to who Echo $this->cpu. ‘--‘ . $this->mouse. ‘---‘ . $this->keyboard; } } $c = new computer (); $c->showinfo ()//// echo $c->cpu;///error, private properties cannot be accessed outside the class //Echo $c->keyboard; Echo $c->mouse;
3. Constructors
<?php /* Constructor: 1, when defined, his function name: __construct 2,PHP4 constructor name is the same name as the class 3, the constructor does not need to be called manually, automatically called When the object is instantiated, the constructor is automatically called */ class person{public $name; public $sex; Public function __construct ($n, $s) { echo ' I will automatically call '; $this->name = $n; $this->sex = $s; } } $p = new Person (' Zhangsan ', ' man ');//Automatic call $p 2 = new person (' Lisi ', ' man ');? >
4. Inheritance
methods for defining subclasses:Class subclass name extends parent class name {}
What did the inheritance inherit? 1, parent class all public properties and Method 2, parent class all protected properties and Method 3, private properties and methods do not inherit.
Public: Within the class, outside the class, the subclass can access the protected: Within the class, the subclass of the class can access the private: Within the class can access
<meta charset= "UTF-8" ><?php class Person {public $name; Public $age; Public function __construct ($n, $a) {$this->name = $n; $this->age = $a; } public Function speak () {echo ' Speak <br/> '; } protected function Eat () {echo ' Eat <br/> '; } Private Function Waimao () {echo ' looks <br/> '; }}//class class name extends parent class name/* Inheritance exactly what? 1, the parent class all public properties and Method 2, the parent class all the properties of the protected and Method 3, the private properties and methods do not inherit over public: in class, outside the class, subclasses can access Protected: Within a class, subclasses can access private: Within a class, you can access the */class Student extends person{
Add content public $No;
Extension constructor Public function __construct ($n, $a, $no) {//parent: Parent class Parent::__construct ($n, $a); Call the parent class's constructor $this->no = $no;//constructor new expanded content}//override constructor//Public function __construct ($n, $a, $no) {//$this->name = $n; $this->age = $a; $this->no = $no; }//If you are not satisfied with the method of the parent class, you can override (overwrite inherited methods) Public function speak () {echo ' I'm talking about student talk <br/> '; }//The parent class does not have a method that can increase the public function test () {echo ' My study number is: '. $this->no. ' Students want exams <br/> '; $this->eat (); The protected method can inherit the quilt class, but cannot access//$this->waimao () outside of the class; Private Modified method/property cannot be inherited}} $p = new person (' Xiao Qiang ', 20); $p->speak (); $stu = new Student (' Big Strong ', 22, ' 2342343 '); $stu->speak (); $stu->test (); $stu->eat ();//Error protected method can inherit the quilt class, but not outside the class access//$stu->waimao ();//Error The method/property of the private modification cannot beCannot access?> outside of the class
5. Encapsulation with Class (database connection, database curd operation)
Learn about the array of PHP you need to use before packaging
array1: []: Empty index (default is the maximum index of the array plus 1 when the index is empty)
<?php $user = Array (); $user [0] = ' zhangsan '; $user [2] = ' Zhangsan '; [] is the largest numeric index on the array +1 //$user [] = ' Zhangsan '; $user [8] = ' Zhangsan '; $user [] = ' Zhangsan '; $user [] = ' Zhang San '; $user [] = ' Lisi '; $user [] = ' Wangwu '; Print_r ($user);//array ([0] = Zhang San [1] = lisi [2] = = Wangwu) //echo $user;? >
array2:array_values and Array_values and implode
1. Array name _values: Takes the value of the array out to be stored in an array and returns
2. Array name _keys: Takes the key of the array out to save in an array and returns
3.implode: Put all the elements in the array into a string. (equivalent to JS join)
<?php$user = Array ( ' title ' = ' star ', ' content ' = ' little Star ', ' age ' = '); Print_r (Array_keys ($user));// $keys = Array_keys ($user); Implode--->join //Echo Implode (",", $keys); $values = Array_values ($user);p rint_r ($user); echo "</br>"; Print_r ($values)/// Echo implode ("', '", $values);//stars, Little stars,22?>
array3:foreach is used to traverse Array,count ($array) to get the length of the array (number of key-value pairs)
foreach usage: foreach (Traverse object as Key = = value) {function executed per traversal}
<meta charset= "UTF-8" > <?php $userList = Array (0 = = Array (' Zhang San ', ' John Doe ', ), 1 = = Array (' Small Stars ', ' small powerful '); foreach ($userList as $k = + $v) {//Echo $k. '---> '. $v [0]. '---> '. $v [1]. ' <br/> '; } foreach ($userList as $key = + $val) {//Echo $k. '---> '; Echo $key; foreach ($val as $k 2 = + $v 2) {echo '----> '. $V 2; } echo ' <br/> '; }//$arr = Array (//0 = 1,//3 = ten,//5 = +,//);//foreach ($arr as $key = = $val) {//foreach ($arr as $k + $v) {//Echo $key. '--'. $val. ' <br/> ';//Echo $val. ' <br/> ';//Echo $v. ' <br/> ';//}//foreach ($arr as $val) {//Echo $val. ' <br/> '; }//Echo count ($arr); 3//$i---> 0, 1, 2//for cycle missingPoint: Cannot traverse discontinuous numeric index and string index//for ($i = 0; $i < count ($arr); $i + +) {//$arr [0] $arr [1] $arr [2]//Echo $arr [$i]. ' <br/> '; }?>
Encapsulating database connections, database curd operations (new mysql.class.php)
New Database sxlxb, table message, column name (msg_id,title,content) required for testing
<meta charset= "UTF-8" ><?php class Mysql {private $host;//host name private $dbName;//Database name P Rivate $userName; User name private $userPWd; Password Public function __construct ($_host, $_dbname, $_username, $_userpwd) {$this->host = $_host; $this->dbname = $_dbname; $this->username = $_username; $this->userpwd = $_userpwd; if (! $this->connect ()) {Die (Mysql_error ()); }//Else{echo "Connect is OK";} Test whether the database is connected successfully//set encoding $this->setcode (); Select Database $this->selectdb (); Public Function Connect () {return mysql_connect ($this->host, $this->username, $this->userpwd ); Public Function Setcode () {$this->query ("Set names UTF8"); } public Function Selectdb () {mysql_select_db ($this->dbname); } Public Function Query ($sql) {//EXECUTE statement return mysql_query ($sql); }//is used to query all data public function GetAll ($sql) {$res = $this->query ($sql);//Return Resource $l ist = Array (); while ($row = Mysql_fetch_assoc ($res)) {//traverse out each line of resources//Array_push ($list, $row); $list [] = $row; } return $list; }//Query one row of data public function GetRow ($sql) {$res = $this->query ($sql); Return Mysql_fetch_assoc ($res); }//Query the number of a column public function Getcol ($sql) {$res = $this->query ($sql); $row = Mysql_fetch_row ($res); return $row [0]; }//inserts//INSERT into message (title, content) VALUES (' stars ', ' little Stars '); Product Manager----> Pain Point/* Add (Array (' title ' = ' star ', ' content ' = ' small Star ') Star '), ' message '); */ Public function Add ($data, $tbName) {$sql = ' INSERT into {$tbName} '; INSERT into message ($sql. = Implode (', ', Array_keys ($data)). ") VALUES ('"; $sql. = Implode ("', '", Array_values ($data)). "‘)"; Echo $sql; return $this->query ($sql); }//Update message SET title = ' Hello ', content = ' Hi ' WHERE msg_id = 1 public Function UPDATE ($data, $tbN Ame, $condition) {$sql = "UPDATE {$tbName} SET"; foreach ($data as $k = = $v) {$sql. = $k. ' = '. "' $v ',"; }//UPDATE message SET title = ' Hello ', content = ' Hi ', $sql = substr ($sql, 0,-1); $sql. = "". $condition; return $this->query ($sql); }} $mysql = new MySQL ("localhost", "sxlxb", "root", "root"); $list = $mysql->getall ("SELECT * from message"); $list = $mysql->getall ("Select title from Message"); Print_r ($list); $msgDetail = $mysql->getrow ("SELECT * from message WHERE msg_id = 2"); Print_r ($msgDetail); $total = $mysql->getcol ("Select COUNT (msg_id) as total from message;"); Echo $total; $res = $mysql->add (Array (//' title ' = ' simplified insert ',//' content ' = ' Now insert data very comfortable '//), ' message ‘); $res = $mysql->update (Array (//' title ' + ' Simplified modification ',//' content ' + ' now modifies data very comfortable '//), ' mess Age ', ' where user_id = 8 '); echo $res//Echo ($res!== false)? ' Update is OK ': ' Error ';? >
Front-end small White's daily learning record----PHP (6) Object-oriented