Php-data-type instance tutorial _ PHP-php Tutorial

Source: Internet
Author: User
Php Class Data class instance tutorial. In this section, we will kill two birds and one stone. Not only will we learn how to integrate object-oriented PHP and MySQL, but we will also learn how to group objects. In this case, you will build one in this section, and we will kill two birds and one stone. Not only will we learn how to integrate object-oriented PHP and MySQL, but we will also learn how to group objects. In this case, you will create a first-class UsersGroup which will contain a series of user objects. Each user object is created for continuous use query from MySQL. To try this example, you first need to create a user table in the MySQL database. I used the MySQL database named 'kupa_oop '. Use the following query to create a table and insert sample data.

/* Create Users table */
Create table 'users '(
'User _ id' int not null AUTO_INCREMENT,
'User _ name' text not null,
Primary key ('User _ id ')
);

/* Insert sample data into Users table */
Insert into 'users'
('User _ id', 'User _ name ')
VALUES
('', 'Kirupa '),
('', 'Bwh2 ');

The simple table contains two columns: 'user_id 'and 'username '. You may wish to add your own name. This table is a spice thing.

As promised, we will create a UsersGroup class. The workgroup will contain a series of user objects, each of which creates tables using data from our MySQL. The database name on all my tables is 'kupa_oop '. If your database does not have the name 'kupa_oop ', you must change the settings class in the database name. Here is our UsersGroup class action:

$ Group = new UsersGroup;

/* Loop through our group, echo user names */
Foreach ($ group-> getUsers () as $ user ){
Echo $ user-> getName ().'
';
}

/* User class, same as before */
Class User {
Private $ name;

Function _ construct ($ attribs ){
$ This-> name = $ attribs ['name'];
}

/* Name methods */
Function setName ($ val ){
$ This-> name = $ val;
Return;
}

Function getName (){
Return $ this-> name;
}
}

/* Contains a group of User objects */
Class UsersGroup {
Private $ name; // name of group
Private $ group = array (); // group of User objects

Function _ construct (){
/* Connect to DB using Settings */
$ Link = mysql_connect (
Settings: $ DATABASE ['host'],
Settings: $ DATABASE ['username'],
Settings: $ DATABASE ['password']
);
Mysql_select_db (Settings: $ DATABASE ['database'], $ link );

/* Get table names from Settings class */
$ Tbl_users = Settings: $ TABLES ['tbl _ users'];

/* Query */
$ SQL = "SELECT user_id AS ID,
User_name AS name
FROM $ tbl_users ";
$ Result = mysql_query ($ SQL) or die (mysql_error ());

/* Adds user to group with each row of data */
While ($ row = mysql_fetch_array ($ result )){
$ This-> addUser ($ row );
}
}

/*
Add a user to Group
Does simple check to see if we pass an array (like $ attribs)
Or if we pass an object (like a User object)
*/
Function addUser ($ user ){
If (is_object ($ user )){
Array_push ($ this-> group, $ user );
}
If (is_array ($ user )){
$ Noob = new User ($ user );
Array_push ($ this-> group, $ noob );
}
Return;
}

/* Returns the group (which is an array )*/
Function getUsers (){
Return $ this-> group;
}
}

/* Holds our site settings */
Class Settings {
Static $ DATABASE = array (
// Change these as needed 'database' => 'kirupa

The first thing to note is that we hold our database connection and data table names in China to set first-class static variables. If we need to change our database name, table name, database username, and so on, we can only do it, we set first-class (this is best in a single, including files ). What's New at the user level-it not only owns the name of the property, but also obtains a series of data via _ construct.

Let's take a look at the code UsersGroup class. If you have already connected to the MySQL database and PHP, what should be new. The only difference is that we use static variables for our connection settings. We do this for our name. because of the double quotation marks in the table, our first-class static variables cannot be parsed. That is to say, an error occurs only when you use the settings: $ Table ['tbl_users '] to query SQL statements directly in our double quotation marks.

Our SQL query generates a series of rows, which we specify as results. Using the while loop, we use our $ result array. The result of USD per element is a series of values (such as numbers and names ). Similarly, it looks familiar to people who use PHP to access the MySQL database. The current object-oriented part: we then use each row (column) to query our addUser method from our MySQL. First, check the addUser method if it is passed through a series of data (such as $ row) or the entire user object. In this case, we are using a series of data. Using the data of this array, addUser creates a user object ($ noob), and then adds that the user object group array ($ group ).

Note: When our user's goal is to create a series of data, the structure is to find the name of the so-called array element. To meet this requirement, we can use the SQL alias to view the USD database. If we do not have the alias username dollar continuous variables, the so-called array element name will not be held, but the username. Therefore, it does not correctly fill in the variable name in our user object. I would rather let me customize SQL than my PHP class methods, because I am more likely to reuse PHP class methods than SQL queries. For example, if I create a dog class, I also want the getName method and construction. Instead of modifying several PHP methods to support users rather than dogs, I would rather modify an SQL query. In addition, you can rename your username column, which is just the name. This will maximize reuse, but it may hurt your ability to understand more complex SQL queries and database designs.

You may consider this type as being too complex. After all, you may code all of this in 15 rows and often use PHP pages. Therefore, what are the advantages? Okay, let's see how we use our objects:

/* Instantiate group */
$ Group = new UsersGroup;

/* Loop through our group, echo user names */
Foreach ($ group-> getUsers as $ user ){
Echo $ user-> getName ().'
';
}

Bytes. Not only will we learn how to integrate object-oriented PHP and MySQL, but we will also learn how to group objects. In this case, you will create...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.