Beginning PHP 5.3 phần 6

Như bạn có thể thấy, PHP cung cấp cho bạn nhiều cách khác nhau để đọc và viết các tập tin, vì vậy bạn luôn có thể tìm thấy một chức năng để phù hợp với nhu cầu của bạn! Ghi một chuỗi toàn bộ vào một tập tin mà không cần phải mở nó fpassthru () - | Part III Using PHP in Practice Creating the DataObject Class File Now comes the first of the classes that are used in the application. DataObject is an abstract class from which you can derive classes to handle database access and data retrieval. Because it s an abstract class you can t instantiate create objects from it directly. In a moment you create classes to handle both members and access log records that are based on the DataObject class. In OOP parlance these types of classes are said to follow the active record design pattern which means that the object contains the data for the record to store in or retrieve from the database as well as the methods to carry out the actual storage and retrieval. Save the following script as in the book_club folder php require_once abstract class DataObject protected data array public function __construct data foreach data as key value if array_key_exists key this- data this- data key value public function getValue field if array_key_exists field this- data return this- data field else die Field not found public function getValueEncoded field return htmlspecialchars this- getValue field protected function connect try conn new PDO DB_DSN DB_USERNAME DB_PASSWORD conn- setAttribute PDO ATTR_PERSISTENT true conn- setAttribute PDO ATTR_ERRMODE PDO ERRMODE_EXCEPTION catch PDOException e die Connection failed . e- getMessage return conn protected function disconnect conn 388 Chapter 13 Retrieving Data from MySQL with PHP conn So how does this class work First of all the script includes the file so that it can access the database configuration constants require_once The PHP require_once function imports another PHP script into the current script in a similar way to require which you ve used in previous chapters. The difference is that require_once ensures that the file is imported only once. This is important if you create a large application with lots of script files many of .

Không thể tạo bản xem trước, hãy bấm tải xuống
TÀI LIỆU MỚI ĐĂNG
145    382    5    14-05-2024
169    69    2    14-05-2024
Đã 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.