Danh mục

PHP Objects, Patterns, and Practice- P6

Số trang: 50      Loại file: pdf      Dung lượng: 0.00 B      Lượt xem: 14      Lượt tải: 0    
Thư viện của tui

Xem trước 5 trang đầu tiên của tài liệu này:

Thông tin tài liệu:

PHP Objects, Patterns, and Practice- P6: This book takes you beyond the PHP basics to the enterprise development practices used by professional programmers. Updated for PHP 5.3 with new sections on closures, namespaces, and continuous integration, this edition will teach you about object features such as abstract classes, reflection, interfaces, and error handling. You’ll also discover object tools to help you learn more about your classes, objects, and methods.
Nội dung trích xuất từ tài liệu:
PHP Objects, Patterns, and Practice- P6 CHAPTER 12 ■ ENTERPRISE PATTERNS When you need to put your system through its paces, you can use test mode to switch in a fakeregistry. This can serve up stubs (objects that fake a real environment for testing purposes) or mocks(similar objects that also analyze calls made to them and assess them for correctness).Registry::testMode();$mockreg = Registry::instance(); You can read more about mock and stub objects in Chapter 18, “Testing with PHPUnit.”Registry, Scope, and PHPThe term scope is often used to describe the visibility of an object or value in the context of codestructures. The lifetime of a variable can also be measured over time. There are three levels of scope youmight consider in this sense. The standard is the period covered by an HTTP request. PHP also provides built-in support for session variables. These are serialized and saved to the filesystem or the database at the end of a request, and then restored at the start of the next. A session IDstored in a cookie or passed around in query strings is used to keep track of the session owner. Becauseof this, you can think of some variables having session scope. You can take advantage of this by storingsome objects between requests, saving a trip to the database. Clearly, you need to be careful that youdon’t end up with multiple versions of the same object, so you may need to consider a locking strategywhen you check an object that also exists in a database into a session. In other languages, notably Java and Perl (running on the ModPerl Apache module), there is theconcept of application scope. Variables that occupy this space are available across all instances of theapplication. This is fairly alien to PHP, but in larger applications, it is very useful to have access to anapplicationwide space for accessing configuration variables. You can build a registry class that emulatesapplication scope, though you must be aware of some pretty considerable caveats. Figure 12–3 shows a possible structure for Registry classes that work on the three levels I havedescribed.Figure 12–3. Implementing registry classes for different scopes 229 CHAPTER 12 ■ ENTERPRISE PATTERNS The base class defines two protected methods, get() and set(). They are not available to client code, because I want to enforce type for get and set operations. The base class may define other public methods such as isEmpty(), isPopulated(), and clear(), but I’ll leave those as an exercise for you to do. ■Note In a real-world system, you might want to extend this structure to include another layer of inheritance. You might keep the concrete get() and set() methods in their respective implementations, but specialize the public getAaa() and setAaa() methods into domain-specific classes. The new specializations would become the singletons. That way you could reuse the core save and retrieve operations across multiple applications. Here is the abstract class as code: namespace wooase; abstract class Registry { abstract protected function get( $key ); abstract protected function set( $key, $val ); } ■Note Notice that I’m using namespaces in these examples. Because I will be building a complete, if basic, system in this chapter, it makes sense to use a package hierarchy, and to take advantage of the brevity and clarity that namespaces can bring to a project. The request level class is pretty straightforward. In another variation from my previous example, I keep the Registry sole instance hidden and provide static methods to set and get objects. Apart from that, it’s simply a matter of maintaining an associative array. namespace wooase; // ... class RequestRegistry extends Registry { private $values = array(); private static $instance; private function __construct() {} static function instance() { if ( ! isset(self::$instance) ) { self::$instance = new self(); } return self::$instance; } protected function get( $key ) {230 CHAPTER 12 ■ ENTERPRISE PATTERNS if ( isset( $this->values[$key] ) ) { return $this->values[$key]; } return null; } protected function set( $key, $val ) { $this->values[$key] = $val; } static function getRequest() { return self::instance()->get(request); } static function setRequest( woocontrollerRequest $request ) { ...

Tài liệu được xem nhiều: