Monday, 16 May 2016

Object Oriented Programming Concepts With MySql Data Base

Developers who have already written code to get data in and out of the mysql database, but who wish to discoverif there are any benefits of adopting an Object Oriented approch.

This exercise will help in:

  • Defining a class with properties and methods.
  • Creating an object (an instance) of class.
  • Creating a class which extends another class.
  • Communicating with an object from within a PHP script.

The focus is to develop a class that can be used in applications that require connectivity and interaction with the MySql Db engine.

CLASS NAMEdharmarajDbClass
ConstantsMYSQL_TYPES_NUMERIC
MYSQL_TYPES_DATE
MYSQL_TYPES_STRING
Properties
Methods
Parameters
Purpose
last_error__construct()N/A 
last_queryconnect()host, user, pw, db, persistantTo establish a Connection with MySql Db server. Also allows selecting a start up database.
row_countselect_db()dbMySql database name for selectionTo select a database to work with. this can be called when it is desired to work a different Db.
hostselect()sqlAn Sql queryTo execute an SQL query passed as a parameter.This function returns the result pointere.
userselect_one()sqlAn Sql queryPerforms an SQL query to return only ONE column and one result.
pwget_row()$resultA result pointer returned after query executation.To retrieve a row of data from the query result set.
$typeMYSQL_ASSOC or MYSQL_NUM or MYSQL_BOTH(Default)
dbdump_query()sqlAn Sql queryTo display the query result in table format.
db_linkinsert_sql()sqlAn Sql queryTo accept an Insert SQL statement and fire the same against the database.
auto_slashesupdate_sql()sqlAn Sql queryTo accept and UPDATE SQL statement and fire the same against the database.
 insert_array()$tableThe table name to which the INSERT will be fired.To inserte data accepted (as a parameter) in form of an array to a specific Db table (accepted as a parameter).
$dataThe array holding values (to be inserted).
 update_array()$tableThe table name against which the Update will be fired.To update data accepted (as a parameter) in form of an array against a specific Db table (accepted as a parameter) based on a specific condition (accepted as a parameter) 
$dataThe array holding values (to be updated)
$conditionA WHERE clause condition that can be applied to the UPDATE SQL statement.
 execute_file()$fileThe file name (along with path if required) holding the SQL commands.To execute SQL commands held inside an external file.
 get_column_type()$tableThe table name to which INSERT will be fired.To determine the data type of a particular column.
$columbThe column name whose data type has to determiner.
 sql_date_format()$valueThe data srtingTo convert a string to a data formate for insertion in the database.
 print_last_error()$show_queryIf this holds true the last query that was executed is also displayed along.sTo print the last error to the screen.
 print_last_query()NONETo display the last query that was executed.
    

The following code spec creats a Class named dharmarajDbClass in dbClass.php file. 
To be Continued....


Simple, non-OOP programs may be one "long" list of statements (or commands). More complex programs will often group smaller sections of these statements into functions or subroutines each of which might perform a particular task. With designs of this sort, it is common for some of the program's data to be 'global', i.e. accessible from any part of the program. As programs grow in size, allowing any function to modify any piece of data means that bugs can have wide-reaching effects.

In contrast, the object-oriented approach encourages the programmer to place data where it is not directly accessible by the rest of the program. Instead, the data is accessed by calling specially written functions, commonly called methods, which are either bundled in with the data or inherited from "class objects." These act as the intermediaries for retrieving or modifying the data they control. The programming construct that combines data with a set of methods for accessing and managing those data is called an object. The practice of using subroutines to examine or modify certain kinds of data, however, was also quite commonly used in non-OOP modular programming, well before the widespread use of object-oriented programming.

An object-oriented program will usually contain different types of objects, each type corresponding to a particular kind of complex data to be managed or perhaps to a real-world object or concept such as a bank account, a hockey player, or a bulldozer. A program might well contain multiple copies of each type of object, one for each of the real-world objects the program is dealing with. For instance, there could be one bank account object for each real-world account at a particular bank. Each copy of the bank account object would be alike in the methods it offers for manipulating or reading its data, but the data inside each object would differ reflecting the different history of each account.

Objects can be thought of as wrapping their data within a set of functions designed to ensure that the data are used appropriately, and to assist in that use. The object's methods will typically include checks and safeguards that are specific to the types of data the object contains. An object can also offer simple-to-use, standardized methods for performing particular operations on its data, while concealing the specifics of how those tasks are accomplished. In this way alterations can be made to the internal structure or methods of an object without requiring that the rest of the program be modified. This approach can also be used to offer standardized methods across different types of objects. As an example, several different types of objects might offer print methods. Each type of object might implement that print method in a different way, reflecting the different kinds of data each contains, but all the different print methods might be called in the same standardized manner from elsewhere in the program. These features become especially useful when more than one programmer is contributing code to a project or when the goal is to reuse code between projects.

No comments:

Post a Comment