Home  >  Article  >  Backend Development  >  PHP object-oriented object concept understanding_PHP tutorial

PHP object-oriented object concept understanding_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 17:40:22779browse

Simple understanding
Image "It's like two people fighting. You have to use a program to describe it. It's structure-oriented. You have written down every step of the fight, such as what moves the other party uses, Every move you perform must be written down in an object-oriented way. You have to split the fighting process into several parts, before the fight, during the fight, and after."

Now I have been studying the object-oriented approach of PHP for some time, and some of the systems I am building are also built using object-oriented methods.

Let’s talk about the benefits of object-oriented. It can make the system very modular, allow many programmers to work together, and improve the efficiency of coding. It is also much easier to maintain and update the entire system.

Post a category below for everyone to experience. This is a basic class for database linking and operations. It provides references for other classes.
/*
Database class file: class_database.php

Database operation class, this class is the basis for other class operations, that is, the implementation of other class functions is generally Implemented through database class

Created on May 17, 2007
*/
include_once("config.inc"); //Include system configuration file
class data_class
{
//Attributes
private $host; //Server name
private $user; //Username
private $pwd; //Password
private $name; //Database name
private $connection; //Connection identifier

//Method
//__get(): Get property value
function __get($property_name){
if(isset($ this->$property_name))
                                                                                                                 }
}

//__set(): Set a single private data attribute value, used to modify a small amount of data
function __set($property_name, $value)
{
    $this->$property_name = $value;
}

//__construct: Constructor, establishes connection, automatically calls the establishment when the function is established. In principle, when creating a new object, do not explicitly call
function __construct()
{
$this->host=sys_conf::$dbhost; //Use static attributes of the sys_conf class
$this->user=sys_conf::$dbuser;
$this->pwd= sys_conf::$dbpswd;
                                $this->name=sys_conf::$dbname; ,$this->user,$this->pwd);//Establish connection
mysql_query("set names utf8");//Unification of character sets
mysql_select_db("$this->name ", $this->connection); //Select the database challenge cup
}
//__destruct: destructor, disconnect, automatically call destructor when the function is executed. Achieve closing the database connection and ensure the security of data

database data
function __destruct()
{
mysql_close($this->connection);
}

//Add, delete and modify: The parameter $sql is Insert update
function execute($sql)
{
mysql_query($sql);
//echo "Writing to the database was successful";
//echo "I am the execute function of the dataclass class";
}//execute
//Check: The parameter $sql is the Insert statement
//The return value is an object array, each element in the array An object whose one element is one row of records > $query_result=@mysql_query($sql,$this->connection); //Query data
while($row=@mysql_fetch_object($query_result))
{
$resul t_array[$i] =$row;
}//while
      return $result_array;
}

//Get the record number function of the query result
function result_query($sql)
{
$result=mysql_query($sql);
$result_c=mysql_num_rows($result);
return $result_c;
}
}
?> >

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/486230.htmlTechArticleSimply understand the image "It's like two people fighting. You have to use a program to describe it. It's structure-oriented. You are Write down every step of the fight, such as what moves the opponent will use, what moves you will use...
Statement:
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