tailieunhanh - PHP Object-Oriented Solutions phần 9

Giao diện đếm được bao gồm duy nhất của phương pháp tính (), nhu cầu để trả lại số đếm được của các yếu tố trong đối tượng. Kể từ khi lớp Pos_MysqlImprovedResult là một vỏ bọc cho một đối tượng mysqli_result, phương pháp tính () trả về | Simpo PDF Merge and Split Unregistered Version - http PHP OBJECT-ORIENTED SOLUTIONS already been accessed. You can tell if the loop has already been run if the _key property has a value. So if _key is not null the loop is reset to the first row by calling mysqli_result data_seek and passing it 0 as an argument. The rewind method looks like this public function rewind __ . if is_null this- _key this- _result- data_seek 0 this- _key 0 this- _current this- _result- fetch_assoc this- _valid is_null this- _current false true That s all there is to implementing the Iterator interface. The Countable interface is even easier. Implementing the Countable interface The Countable interface consists solely of the count method which needs to return the number of countable elements in the object. Since the Pos_MysqlImprovedResult class is a wrapper for a mysqli_result object the count method returns the value of the mysqli_result- num_rows property like this public function count return this- _result- num_rows That completes the Pos_MysqlImprovedConnection and Pos_MysqlImprovedResult class definitions. Now it s time to make sure they work as expected. Testing the database classes This brief exercise tests the Pos_MysqlImprovedConnection and Pos_MysqlImprovedResult classes. If you haven t created your own versions you can find fully commented versions of both classes in the finished_classes folder of the download files. You can use any MySQL database of your own for this test. Alternatively you can load the blog table from or for MySQL in the download files. 1. Create a file called in the ch8_exercises folder. You need both the Pos_MysqlImprovedConnection and Pos_MysqlImprovedResult classes so include them at the top of the script like this require_once . Pos require_once . Pos 2. In a try block create an instance of Pos_MysqlImprovedConnection and pass it the database .

crossorigin="anonymous">
Đã phát hiện trình chặn quảng cáo AdBlock
Trang web này phụ thuộc vào doanh thu từ số lần hiển thị quảng cáo để tồn tại. Vui lòng tắt trình chặn quảng cáo của bạn hoặc tạm dừng tính năng chặn quảng cáo cho trang web này.