Table of Contents
Do you want to work on Monday -PHP MySQLi, -phpmysqli
Home Backend Development PHP Tutorial Whether to work on Monday -PHP MySQLi, -phpmysqli_PHP Tutorial

Whether to work on Monday -PHP MySQLi, -phpmysqli_PHP Tutorial

Jul 12, 2016 am 09:03 AM
mysqli

Do you want to work on Monday -PHP MySQLi, -phpmysqli

hi

I was originally ambitious to work, but unexpectedly, something unexpected happened. It was cloudy in the morning and I couldn't get up. At noon, I went back to the dormitory to fiddle with clothes (I'm a female worker, so I'm not awesome) and didn't take a nap. I was in such a confused state. , how to do scientific research, just write this.

1. OOP programming in PHP

4.7 Polymorphism

--Definition

Since there are various method implementations of interfaces, this feature is called polymorphism

--Chestnut

function eat($obj){
if($obj instanceof ICanEat){
$obj->eat("FOOD"); // You don’t need to know whether it is Human or Animal, just eat it
}else{
echo "Can't eat!n";
}
}

$man = new Human();
$monkey = new Animal();

// The same code behaves differently when passing in different implementation classes of the interface. That's why it becomes polymorphic.
eat($man);
eat($monkey);

--Summary

/**
* Polymorphism
* 1. As long as an object implements the interface (instanceof), you can directly call the interface method on the object
*/

4.8 Abstract Class

--Question

Some methods of the classes connected to the interface are the same, so is it possible to allow them to be implemented in the interface instead of being implemented in the class?

For example, humans and animals eat different things but breathe the same.

--Chestnut

abstract class ACanEat{ //Keyword changes
abstract public function eat($food);//If the class needs to implement it by itself, add the abstract keyword in front of it

public function breath(){
echo "Breath use the air.
";
}

}

class Human extends ACanEat{ //implements are used to implement the interface, and extends
public function eat($food){
echo "Human eating ".$food."
";
}
}

class Animal extends ACanEat{ //implements are used to implement the interface, here 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 methods in the class to be temporarily not implemented concretely. We call these methods abstract methods
* 2. Once there are abstract methods in a class, the class must be abstract Class
* 3. Abstract classes, like interfaces, cannot be directly instantiated into objects
*/

5. Magic methods

5.1 Introduction

Note that all magic methods are preceded by two underscores__

Special to OOP in PHP.

Such as constructor and destructor.

5.2 __tostring() and __invoke()

--Definition

__tostring(), this method will be automatically called when the object is used as String; echo $obj;

__invoke(), when the 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 "< br/>".$x;
}
}
$obj=new MagicTest();
echo $obj;

$obj(5);

Usage is similar to constructor and destructor. More automated (automatically called, even if not declared), but at the same time more error-prone, be careful.

5.3 __call() and __callStatic() or overloading

--Definition

When the object accesses a method name that does not exist, __call() will be automatically called;

When the object accesses a non-existent static method name, __callStatic() will be automatically called;

These two methods are also called overloading (different from rewriting); through these two methods, calls with the same method name can be implemented corresponding to different methods

--Chestnut

/*
* tostring() magic method
* invoke() magic method
*/

class MagicTest{
public function __toString(){
return "This is the class magictest.";
}
public function __invoke($x){
echo "< br/>".$x."
";
}
public function __call($name,$arguments){ //The format of __call is fixed, first The first is the method name, and the second is the parameters 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 required when defining methods is fixed.

5.4 __get()__set()__isset()__unset

--Definition

These methods are also called the magic methods of Attribute overloading.

__set(), called when assigning a value to inaccessible attributes (one is that the attribute is undefined, the other is that there is no access permission, such as private) ;

__get(), called when reading the value of an inaccessible attribute;

__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 "< br/>".$x."
";
}
public function __call($name,$arguments){ //The format of __call is fixed, the first one is the method name, the second one 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 must have name
return "Getting the property ".$name."
" ;
}
public function __set($name,$value){ //set must have a name and value
echo "Setting the property ".$name." to value ". $value.".
";
}
public function __isset($name){ //Determine whether the attribute is defined
echo "__isset invoked
";
return true;
}
public function __unset($name){ //Undo
echo "unsetting protery ".$name."< br/>";
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, actually isset and empty have opposite operations when calling __isset.

Then, __set($name, $value) and __unset($name) are a pair of opposite operations, but the required elements are different;

__isset($name), __get($name)all only require names (remember the function of each magic method, it will be easier to remember if you understand it).

5.5 __clone()

--Definition

It’s cloning, or cloning

--Chestnut

First give the usage of clone keyword.

/*
* Clone 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 cloning, it is a separate object, and operations on it do not affect the original object.

Add __clone()

/*
* Clone 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."
";

Generally speaking, it is useful for initialization after cloning; or, concealing certain information that you don’t want to disclose after copying.

I often use this one in my work, because I often operate on an object, and I don’t want to affect the original data , so I just clone/copy it.

----------------------------------------

2. MySQLi extension

1. Installation and download

1.1 Advantages and Introduction

Updated and better, PHP5 and later are recommended (or PDO).

--Advantages

Based on OOP and process-oriented use;

Supports prepared statements;

Support transactions.

--Others

Faster. Better security

1.2 Installation and Configuration

--Install

Configure php and open php_mysqli.dll;

Configure extension_dir='ext directory location';

Restart the server.

(I use WAMP, just check the box)

--Verification

/*
* Verify whether 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. Get the currently enabled extensions
print_r(get_loaded_extensions());
echo '
';

---

I’m sleepy, go back and wash up and go to bed. . .

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1078399.htmlTechArticleShould I work on Monday -PHP MySQLi, -phpmysqli hi I was originally ambitious to work, but I didn’t know Something unexpected happened. It was so cloudy in the morning that I couldn’t get up. I went back to the dormitory at noon to fiddle with clothes (I’m a female worker, so awesome...
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Solution to PHP Fatal error: Call to undefined function mysqli_connect() Solution to PHP Fatal error: Call to undefined function mysqli_connect() Jun 23, 2023 am 09:40 AM

When writing web applications using PHP, a MySQL database is often used to store data. PHP provides a way to interact with the MySQL database called MySQLi. However, sometimes when using MySQLi, you will encounter an error message, as shown below: PHPFatalerror:Calltoundefinedfunctionmysqli_connect() This error message means that PHP cannot find my

What should I do if php cannot connect to mysqli? What should I do if php cannot connect to mysqli? Nov 09, 2022 am 10:07 AM

Solution to php unable to connect to mysqli: 1. Open the "php.ini" file; 2. Find "mysqli.reconnect"; 3. Change "mysqli.reconnect = OFF" to "mysqli.reconnect = on".

What is the running file of mysql What is the running file of mysql Apr 11, 2023 am 10:38 AM

The running file of mysql is mysqld; mysqld is an executable file, which represents the Mysql server program. Executing this file can directly start a server process; and mysqld_safe is a startup script, which will indirectly call mysqld and also start a monitor. process.

PHP PDO vs. mysqli: compare and contrast PHP PDO vs. mysqli: compare and contrast Feb 19, 2024 pm 12:24 PM

PDOPDO is an object-oriented database access abstraction layer that provides a unified interface for PHP, allowing you to use the same code to interact with different databases (such as Mysql, postgresql, oracle). PDO hides the complexity of underlying database connections and simplifies database operations. Advantages and Disadvantages Advantages: Unified interface, supports multiple databases, simplifies database operations, reduces development difficulty, provides prepared statements, improves security, supports transaction processing Disadvantages: performance may be slightly lower than native extensions, relies on external libraries, may increase overhead, demo code uses PDO Connect to mysql database: $db=newPDO("mysql:host=localhost;dbnam

PHP Warning: mysqli_connect(): (HY000/2002): Solution to Connection refused PHP Warning: mysqli_connect(): (HY000/2002): Solution to Connection refused Jun 23, 2023 am 08:54 AM

If you encounter the following error message when using PHP to connect to a MySQL database: PHPWarning:mysqli_connect():(HY000/2002):Connectionrefused, then you can try to solve this problem by following the steps below. To confirm whether the MySQL service is running normally, you should first check whether the MySQL service is running normally. If the service is not running or fails to start, it may cause a connection refused error. you can

Detailed tutorial on establishing a database connection using MySQLi in PHP Detailed tutorial on establishing a database connection using MySQLi in PHP Jun 04, 2024 pm 01:42 PM

How to use MySQLi to establish a database connection in PHP: Include MySQLi extension (require_once) Create connection function (functionconnect_to_db) Call connection function ($conn=connect_to_db()) Execute query ($result=$conn->query()) Close connection ( $conn->close())

Solution to PHP Fatal error: Call to undefined method mysqli::prepare() Solution to PHP Fatal error: Call to undefined method mysqli::prepare() Jun 23, 2023 am 11:21 AM

When using the mysqli extension to connect and operate a MySQL database, you sometimes encounter the error PHPFatalerror:Calltoundefinedmethodmysqli::prepare(). This error is usually caused by the following reasons: PHP has insufficient support for the mysqli extension; the mysqli extension is not loaded or configured correctly; there are syntax errors in the PHP code; the MySQL server is not correctly configured or running

Solution to PHP Fatal error: Call to undefined function mysqli_stmt_bind_param() Solution to PHP Fatal error: Call to undefined function mysqli_stmt_bind_param() Jun 23, 2023 am 10:43 AM

When developing websites using PHP, database operations are very common. MySQLi is an extension commonly used in PHP to operate MySQL databases. It provides a relatively complete object-oriented interface, procedural interface, and supports operations of prepared statements. But sometimes when we use mysqli's prepared statements, we will encounter such an error: PHPFatalerror:Calltoundefinedfunctionmysqli_stmt_bin

See all articles