Developing Object-Oriented PHP.ppt
《Developing Object-Oriented PHP.ppt》由会员分享,可在线阅读,更多相关《Developing Object-Oriented PHP.ppt(49页珍藏版)》请在麦多课文档分享上搜索。
1、Developing Object-Oriented PHP,PHP-Object Oriented Programming,2,Object-Oriented Programming,Object-oriented programming (OOP) refers to the creation of reusable software objects that can be easily incorporated into multiple programs An object refers to programming code and data that can be treated
2、as an individual unit or component Objects are often also called components,PHP-Object Oriented Programming,3,Object-Oriented Programming,Data refers to information contained within variables or other types of storage structures The functions associated with an object are called methods The variable
3、s that are associated with an object are called properties or attributes Popular object-oriented programming languages include C+, Java, and Visual Basic,PHP-Object Oriented Programming,4,Object-Oriented Programming,Figure 11-1 Accounting program,PHP-Object Oriented Programming,5,Understanding Encap
4、sulation,Objects are encapsulated all code and required data are contained within the object itself Encapsulated objects hide all internal code and data An interface refers to the methods and properties that are required for a source program to communicate with an object,PHP-Object Oriented Programm
5、ing,6,Understanding Encapsulation,Encapsulated objects allow users to see only the methods and properties of the object that you allow them to see Encapsulation reduces the complexity of the code Encapsulation prevents other programmers from accidentally introducing a bug into a program, or stealing
6、 code,PHP-Object Oriented Programming,7,Object-Oriented Programming and Classes,The code, methods, attributes, and other information that make up an object are organized into classes An instance is an object that has been created from an existing class Creating an object from an existing class is ca
7、lled instantiating the object An object inherits its methods and properties from a class it takes on the characteristics of the class on which it is based,PHP-Object Oriented Programming,8,Using Objects in PHP Scripts,Declare an object in PHP by using the new operator with a class constructor A clas
8、s constructor is a special function with the same name as its class that is called automatically when an object from the class is instantiated The syntax for instantiating an object is:$ObjectName = new ClassName();,PHP-Object Oriented Programming,9,Using Objects in PHP Scripts,The identifiers for a
9、n object name: Must begin with a dollar sign Can include numbers or an underscore Cannot include spaces Are case sensitive$Checking = new BankAccount(); Can pass arguments to many constructor functions$Checking = new BankAccount(01234587, 1021, 97.58);,PHP-Object Oriented Programming,10,Using Object
10、s in PHP Scripts (continued),After an object is instantiated, use a hyphen and a greater-than symbol (-) to access the methods and properties contained in the object Together, these two characters are referred to as member selection notation With member selection notation append one or more characte
11、rs to an object, followed by the name of a method or property,PHP-Object Oriented Programming,11,Using Objects in PHP Scripts (continued),With methods, include a set of parentheses at the end of the method name, just as with functions Like functions, methods can also accept arguments$Checking-getBal
12、ance();$CheckNumber = 1022;$Checking-getCheckAmount($CheckNumber);,PHP-Object Oriented Programming,12,Working with Database Connections as Objects,Access MySQL database connections as objects by instantiating an object from the mysqli class To connect to a MySQL database server:$DBConnect = mysqli_c
13、onnect(“localhost“, “dongosselin“, “rosebud“, “real_estate“); To connect to the MySQL database server using object-oriented style:$DBConnect = new mysqli(“localhost“, “dongosselin“,“rosebud“, “real_estate“);,PHP-Object Oriented Programming,13,Instantiating and Closing a MySQL Database Object,This st
14、atement also uses the mysqli() constructor function to instantiate a mysqli class object named $DBConnect$DBConnect = new mysqli(“localhost“, “dongosselin“,“rosebud“, “real_estate“); To explicitly close the database connection, use the close() method of the mysqli class$DBConnect-close();,PHP-Object
15、 Oriented Programming,14,Selecting a Database,Select or change a database with the mysqli_select_db() function Pass two arguments to the mysqli_select_db() function: 1. The variable representing the database connection 2. The name of the database you want to use,PHP-Object Oriented Programming,15,Se
16、lecting a Database (continued),Example of procedural syntax to open a connection to a MySQL database server: $DBConnect = mysqli_connect(“localhost“, “dongosselin“, “rosebud“); mysqli_select_db($DBConnect, “real_estate“); / additional statements that access or manipulate the database mysqli_close($D
17、BConnect); An object-oriented version of the code: $DBConnect = mysqli_connect(“localhost“, “dongosselin“, “rosebud“); $DBConnect-select_db(“real_estate“); / additional statements that access or manipulate the database $DBConnect-close();,PHP-Object Oriented Programming,16,Handling MySQL Errors,With
18、 object-oriented style, you cannot terminate script execution with the die() or exit() functions $DBConnect = mysqli_connect(“localhost“, “dongosselin“, “rosebud“)Or die(“Unable to connect to the database server.“. “Error code “ . mysqli_connect_errno(). “: “ . mysqli_connect_error() . “;,PHP-Object
19、 Oriented Programming,17,Handling MySQL Errors,With object-oriented style, check whether a value is assigned to the mysqli_connect_errno() or mysqli_connect_error() functions and then call the die() function to terminate script execution $DBConnect = new mysqli(“localhost“, “dgosselin“, “rosebud“);
20、if (mysqli_connect_errno()die(“Unable to connect to the database server.“. “Error code “ . mysqli_connect_errno(). “: “ . mysqli_connect_error() . “;,PHP-Object Oriented Programming,18,Handling MySQL Errors,For any methods of the mysqli class that fail (as indicated by a return value of false), term
21、inate script execution by appending die() or exit() functions to method call statements$DBName = “guitars“;$DBConnect-select_db($DBName)Or die(“Unable to select the database.“. “Error code “ . mysqli_errno($DBConnect). “: “ . mysqli_error($DBConnect) . “;,PHP-Object Oriented Programming,19,Executing
22、 SQL Statements,With object-oriented style, use the query() method of the mysqli class To return the fields in the current row of a resultset into an indexed array use: The mysqli_fetch_row() function To return the fields in the current row of a resultset into an associative array use: The mysqli_fe
23、tch_assoc() function,PHP-Object Oriented Programming,20,Executing SQL Statements (continued),$TableName = “inventory“; $SQLstring = “SELECT * FROM inventory“; $QueryResult = $DBConnect-query($SQLstring)Or die(“Unable to execute the query.“. “Error code “ . $DBConnect-errno. “: “ . $DBConnect-error)
24、. “; echo “; echo “MakeModel PriceInventory“; $Row = $QueryResult-fetch_row(); do echo “$Row0“;echo “$Row1“;echo “$Row2“;echo “$Row3“;$Row = $QueryResult-fetch_row(); while ($Row);,PHP-Object Oriented Programming,21,Defining Custom PHP Classes,Data structure refers to a system for organizing data Th
25、e functions and variables defined in a class are called class members Class variables are referred to as data members or member variables Class functions are referred to as member functions or function members,PHP-Object Oriented Programming,22,Defining Custom PHP Classes,Classes: Help make complex
- 1.请仔细阅读文档,确保文档完整性,对于不预览、不比对内容而直接下载带来的问题本站不予受理。
- 2.下载的文档,不会出现我们的网址水印。
- 3、该文档所得收入(下载+内容+预览)归上传者、原创作者;如果您是本文档原作者,请点此认领!既往收益都归您。
下载文档到电脑,查找使用更方便
2000 积分 0人已下载
下载 | 加入VIP,交流精品资源 |
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- DEVELOPINGOBJECTORIENTEDPHPPPT
