Do not work on Monday-PHP + MySQLi hi
I was ambitious enough to work. I could not expect it to happen on some days. I couldn't get up on a cloudy day in the morning. I went back to my bedroom to toss my clothes (as a female, a cow or a cow) and didn't take a nap at noon, this is a confused state, how to research, write this well.
1. php oop programming
4.7 polymorphism
-- Definition
Because there are many methods to implement interfaces, this feature is called polymorphism.
-- Chestnut
Function eat ($ obj ){
If ($ obj instanceof ICanEat ){
$ Obj-> eat ("FOOD"); // You do not need to know whether it is Human or Animal. just eat it.
} Else {
Echo "Can't eat! \ N ";
}
}
$ Man = new Human ();
$ Monkey = new Animal ();
// For the same code, different implementation classes of the input interface are different. This is why polymorphism occurs.
Eat ($ man );
Eat ($ monkey );
-- Summary
/**
* Polymorphism
* 1. as long as an object implements an interface (instanceof), you can directly call the interface method on the object.
*/
4.8 abstract class
-- Problem
Some methods of the connected interface are the same, so whether the class can be not implemented is implemented in the interface.
For example, humans and animals eat differently, but breathe the same thing.
-- Chestnut
Abstract class ACanEat {// keyword change
Abstract public function eat ($ food); // you need to implement the function by yourself.
Public function breath (){
Echo "Breath use the air.
";
}
}
Class Human extends ACanEat {// implenments is used to implement the interface, and extends is used here
Public function eat ($ food ){
Echo "Human eating". $ food ."
";
}
}
Class AnimalExtendsACanEat {// Implenments is used to implement the interface, and extends is used here
Public function eat ($ food ){
Echo "Animal eating". $ food ."
";
}
}
$ Xiaoming = new Human ();
$ Xiaohei = new Animal ();
$ Xiaoming-> breath (); $ xiaoming-> eat ("food ");
$ Xiaohei-> breath (); $ xiaohei-> eat ("shit ");
-- Summary
/**
* Abstract class
* 1. abstract classes allow some methods in the class to be not implemented at the moment. these methods become abstract methods.
* 2. once there is an abstract method in the class, this class must be an abstract class.
* 3. abstract classes, like interfaces, cannot be directly instantiated as objects.
*/
5. magic methods
5.1 Introduction
Note that all magic methods are preceded by two underscores __
OOP exclusive to PHP.
For example, constructor and Destructor.
5.2 _ tostring () and _ invoke ()
-- Definition
_ Tostring (): This method is automatically called when the object is used as a String; echo $ obj;
_ Invoke (): When an object is called as a method (function), this method is automatically called; $ obj (4 );
-- Chestnut
/*
* Tostring () magic method
* Invoke () magic method
*/
Class MagicTest {
Public function _ toString (){
Return "This is the class magictest .";
}
Public function _ invoke ($ x ){
Echo"
". $ X;
}
}
$ Obj = new MagicTest ();
Echo $ obj;
$ Obj (5 );
The usage is similar to that of the constructor. More automated (automatic call, called even if there is no declaration), but at the same timeErrors are easy. be careful.
5.3 _ call () and _ callStatic () or overload (overloading)
-- Definition
When an object accesses a method that does not exist, __call () is automatically called;
When the object accesses a non-existent static method name, __callstatic () will be called automatically;
These two methods are also called overload (different from rewriting). through these two methods, the call of the same method name can correspond to different method implementations.
-- Chestnut
/*
* Tostring () magic method
* Invoke () magic method
*/
Class MagicTest {
Public function _ toString (){
Return "This is the class magictest .";
}
Public function _ invoke ($ x ){
Echo"
". $ X ."
";
}
Public function _ call ($ name, $ arguments) {// _ call format is fixed. The first is the method name, and the second is the parameters in the method.
Echo "Calling". $ name. "with parameters:". implode (",", $ arguments )."
";
}
PublicStaticFunction _ callstatic ($ name, $ arguments ){
Echo "Static calling". $ name. "with parameters:". implode (",", $ arguments )."
";
}
}
$ Obj = new MagicTest ();
Echo $ obj;
$ Obj (5 );
$ Obj-> runTest ("para1", "para2 ");
$ Obj: runTest ("para3", "para4 ");
Note that the format is fixed when the method is defined here.
5.4 _ get () _ set () _ isset () _ unset
-- Definition
These methods are also calledAttribute overloading.
_ Set (), inInaccessible property (one is undefined, and the other is not authorized, such as private)Call when assigning values;
_ Get (), called when reading the value of the inaccessible attribute;
_ Isset (), called when isset () or empty () is called for inaccessible attributes;
_ Unset (),......... Unset ()..........
-- Chestnut
/*
* Tostring () magic method
* Invoke () magic method
*/
Class MagicTest {
Public function _ toString (){
Return "This is the class magictest .";
}
Public function _ invoke ($ x ){
Echo"
". $ X ."
";
}
Public function _ call ($ name, $ arguments) {// _ call format is fixed. The first is the method name, and the second is the parameters in the method.
Echo "Calling". $ name. "with parameters:". implode (",", $ arguments )."
";
}
Public static function _ callstatic ($ name, $ arguments ){
Echo "Static calling". $ name. "with parameters:". implode (",", $ arguments )."
";
}
Public function _ get ($ name) {// get must have name
Return "Getting the property". $ name ."
";
}
Public function _ set ($ name, $ value) {// set must be named with a value
Echo "Setting the property". $ name. "to value". $ value .".
";
}
Public function _ isset ($ name) {// determines whether an attribute is defined
Echo "_ isset invoked
";
Return true;
}
Public function _ unset ($ name) {// undo
Echo "unsetting protery". $ name ."
";
Return true;
}
}
$ Obj = new MagicTest ();
Echo $ obj;
$ Obj (5 );
$ Obj-> runTest ("para1", "para2 ");
$ Obj: runTest ("para3", "para4 ");
Echo $ obj-> classname;
$ Obj-> classname = "shit ";
Echo isset ($ obj-> classname )."
";
Unset ($ obj-> classname); echo"
";
Echo empty ($ obj-> classname )."
";
The result is
This is the class magictest.
5
Calling runTest with parameters: para1, para2
Static calling runTest with parameters: para3, para4
Getting the property classname
Setting the property classname to value shit.
_ Isset invoked
1
Unsetting protery classname
_ Isset invoked
As you can see,In fact, the isset and empty call _ isset operations are opposite.
Then,_ Set ($ name, $ value) and _ unset ($ name)Is a pair of opposite operations, but the elements are not the same;
_ Isset ($ name) ,__ get ($ name)You only need a name (remember the role of each magic method and remember it when you understand it ).
5.5 _ clone ()
-- Definition
Is to clone, or clone
-- Chestnut
FirstClone keyword.
/*
* Clone magic methods
*/
Class nbaPlayer {
Public $ name;
}
$ James = new nbaPlayer ();
$ James-> name = 'James ';
Echo $ james-> name ."
";
$ Kobe = clone $ james;
$ Kobe-> name = 'Kobe ';
Echo $ kobe-> name;
The cloned object is a separate object, and its operation does not affect the original object.
Add _ clone ()
/*
* Clone magic methods
*/
Class nbaPlayer {
Public $ name;
Public function _ clone (){
$ This-> name = "shit ";
}
}
$ James = new nbaPlayer ();
$ James-> name = 'James ';
Echo $ james-> name ."
";
$ Kobe = clone $ james;
Echo $ kobe-> name ."
";
$ Kobe-> name = 'Kobe ';
Echo $ kobe-> name ."
";
In general, it is useful inClone initialization; Or,Some information that you do not want to disclose after the replication is completed.
This is often used in work, becauseOperations on an object do not want to affect the original data.To clone/copy the file.
----------------------------------------
2. MySQLi extension
1. Installation and download
1.1 Advantages and introduction
Better update. PHP5 and later are recommended (or PDO ).
-- Advantages
Based on OOP and process-oriented usage;
Supports preprocessing statements;
Supports transactions.
-- Others
Faster. Better security
1.2 installation and configuration
-- Install
Configure php and enable php_mysqli.dll;
Configure extension_dir = 'ext directory location ';
Restart the server.
(I am using WAMP, just tick it)
-- Verify
/*
* Verify that mysqli is enabled
*/
// Phpinfo ();
// 2. check whether the extension has been loaded
Var_dump (extension_loaded ('mysqli '));
Var_dump (extension_loaded ('curl '));
Echo'
';
// 3. check whether the function exists
Var_dump (function_exists ('mysqli _ connect '));
Echo' ';
// 4. obtain the currently enabled extension
Print_r (get_loaded_extensions ());
Echo' ';
---
Sleepy, go back to wash and sleep...