Monday Do not work-php+mysqli,-phpmysqli
Hi
Originally is ambitious to work, know that day has contingency, morning big cloudy not to come, noon and back to the bedroom toss clothes (do female, cow not cow) didn't nap, such a state of confusion, how scientific research, write this good.
1. OOP Programming for PHP
4.7 polymorphic
--Definition
Because of the variety of methods implemented by interfaces, this characteristic is called polymorphic
--Chestnut
Function Eat ($obj) {
if ($obj instanceof icaneat) {
$obj->eat ("food"); Do not need to know whether it is human or animal, eat directly on the line
}else{
echo "Can ' t eat!\n";
}
}
$man = new Human ();
$monkey = new Animal ();
The same code, the different implementation classes of the incoming interface, behaves differently. That's why it's a polymorphic cause.
Eat ($man);
Eat ($monkey);
--Summary
/**
* polymorphic
* 1. As long as an object implements an interface (instanceof), you can invoke the interface's method directly on the object
*/
4.8 Abstract class
--Questions
The classes that connect interfaces, some of which are the same, are allowed to be implemented in the interface instead of being implemented in the class.
For example, people and animals eat different things, but breathe the same.
--Chestnut
Abstract class acaneat{//keyword change
Abstract public Function eat ($food);//requires class to be implemented by itself, preceded by the abstract keyword
Public Function Breath () {
echo "Breath use the air.
";
}
}
Class Human extends acaneat{//implementation interface with Implenments, here with extends
Public function Eat ($food) {
echo "Human eating". $food. "
";
}
}
Class Animal extends acaneat{ //implementation interface with Implenments, here with extends
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 of the methods inside the class to be temporarily not implemented, and these methods we become abstract methods
* 2. Once the class has an abstract method, the class must be an abstract class
* 3. Abstract classes, like interfaces, cannot be directly instantiated as objects
*/
Five, Magic method
5.1 Introduction
Note that all magic methods are preceded by two underscores __
Specific to OOP in PHP.
For example, constructors and destructors.
5.2 __tostring () and __invoke ()
--Definition
__tostring (), this method is automatically called when the object is used as a string; echo $obj;
__invoke (), this method is automatically called when the object is called as a method (function); $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 the constructor destructor. Compare automation (called automatically, even if no claims are made), but at the same time be more error-prone and careful.
5.3 __call () and __callstatic () or overloaded (overloading)
--Definition
When an object accesses a method name that does not exist, __call () is automatically called;
When an object accesses a static method name that does not exist, __callstatic () is automatically called;
These two methods, also known as overloads (unlike overrides), can be implemented by invoking the name of the same method in a different way.
--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 parameter within the method
echo "calling". $name. "With parameters:". Implode (",", $arguments). "
";
}
Public static function __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 required to be defined.
5.4 __get () __set () __isset () __unset
--Definition
These methods are also known as the Magic method of property overloading .
__set (), called when assigning a value to an unreachable property (one that is not defined and another that does not have access, such as private) ;
__get (), called when reading the value of an inaccessible property;
__isset (), called when isset () or empty () is called on an inaccessible property;
__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 parameter within 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 to have name
Return "Getting the property". $name. "
";
}
Public function __set ($name, $value) {//set to be famous for a value
echo "Setting the property". $name. ' To value '. $value. ".
";
}
Public function __isset ($name) {//Determines whether the 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
You can see that in fact isset and empty call __isset when a pair of opposite operations.
Then,__set ($name, $value) and __unset ($name) are a pair of opposite operations, but the elements are not the same;
__isset ($name), __get ($name) only need names (remember the role of each magic method, understand, it is good to remember).
5.5 __clone ()
--Definition
Is cloning, or cloning
--Chestnut
The use of the clone keyword is given first.
/*
* Cloning Magic Method
*/
Class nbaplayer{
Public $name;
}
$james =new Nbaplayer ();
$james->name= ' James ';
echo $james->name. "
";
$kobe =clone $james;
$kobe->name= ' Kobe ';
Echo $kobe->name;
After clone, it is a separate object, and its operation does not affect the original object.
Plus __clone ()
/*
* Cloning Magic Method
*/
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, the usefulness is the initialization after cloning, or, when copied, do not want to reveal some of the information obscured.
In the work of this one, because often the operation of an object, and do not want to affect the original data , cloning/copying one out.
----------------------------------------
2. mysqli extension
First, installation and download
1.1 Advantages and Introduction
Updates are better, PHP5 and later recommended (or PDO).
--Advantages
Based on OOP and process-oriented use;
Support preprocessing statements;
Support transactions.
--Other
Faster. Better security
1.2 Installation and Configuration
--Installation
Configure PHP to open php_mysqli.dll;
Configure extension_dir= ' ext directory location ';
Restart the server.
(I use a wamp, just tick the line)
--Verification
/*
* Verify that the mysqli is turned on
*/
Phpinfo ();
2. Detect if the extension is already loaded
Var_dump (extension_loaded (' mysqli '));
Var_dump (extension_loaded (' curl '));
Echo '
';
3. Detect if a function exists
Var_dump (function_exists (' mysqli_connect '));
Echo ' ';
4. Get an extension that is currently open
Print_r (Get_loaded_extensions ());
Echo ' ';
---
Sleepy, go back to wash and sleep ...
http://www.bkjia.com/PHPjc/1078399.html www.bkjia.com true http://www.bkjia.com/PHPjc/1078399.html techarticle Monday do not work-php+mysqli,-phpmysqli Hi was originally ambitious to work, but the day has contingency, morning big cloudy not to come, noon and back to the bedroom tossing clothes (do women, cows ...)