Danh mục

PHP: The Good Parts: Delivering the Best of PHP- P5

Số trang: 20      Loại file: pdf      Dung lượng: 309.91 KB      Lượt xem: 37      Lượt tải: 0    
Thu Hiền

Phí tải xuống: 15,000 VND Tải xuống file đầy đủ (20 trang) 0
Xem trước 2 trang đầu tiên của tài liệu này:

Thông tin tài liệu:

PHP: The Good Parts: Delivering the Best of PHP- P5: Vượt qua tất cả các hype về PHP và thâm nhập vào sức mạnh thực sự của ngôn ngữ này. Cuốn sách này khám phá những tính năng hữu ích nhất của PHP và làm thế nào họ có thể đẩy nhanh quá trình phát triển web, và giải thích lý do tại sao thường được sử dụng PHP yếu tố thường được sử dụng sai hoặc misapplied. Bạn sẽ tìm hiểu thêm phần nào sức mạnh để lập trình hướng đối tượng, và làm thế nào để sử dụng...
Nội dung trích xuất từ tài liệu:
PHP: The Good Parts: Delivering the Best of PHP- P5 if ($height) $this->tag .= height= . $height . ; $this->tag .= > ; return $this->tag ; } function ColumnOn($colspan=1, $align=left, $width=, $rowspan=, $bgcolor=, $class=, $valign=, $height=) { $this->tag = if ($name) $this->tag .= name= . $name . ; if ($value) $this->tag .= value= . $value . ; $this->tag .= > ; return $this->tag ; } function InputLabel($textLabel, $labelFor, $required=false, $class=) { if ($required == true) $required = *; $this->tag = ; $this->tag .= $textLabel . $required; $this->tag .= : ; return $this->tag ; } function Input($InputType, $EntityName, $value=, $align=center, $size=, $id=, $align=center, $readonly=, $class=, $onType1=, $onAction1=, $onType2=, $onAction2=, $onType3=, $onAction3=) { $this->tag = } } // end class formIt makes great sense to save these files as separate includable files; I would save themas html_class.inc, table_class.inc, and form_class.inc, respectively, and then includethem—or better still, require them—into the code file where they will be used. Thesethree files are just the object definitions (the classes) and it does look like a lot of codefor little gain (or, as they say in my part of the world, trying to drive in a thumbtackwith a sledgehammer).In PHP, if you want to create (instantiate) an active class (an object) use the new key-word. Here is an example of the top part of a file that is using and instantiating all threeclasses: // require classes for the page require_once (classes/html_class.inc); require_once (classes/table_class.inc); require_once (classes/form_class.inc); // instantiate classes (prepare them for use) $HTMLPage = new html(GuestBook Page) ; $MyTable = new table() ; $MyForm = new form() ;Next, we want to start using the methods within these classes to build a web page thatwill accept a first name with a display of 30 characters, a last name with a display of 40characters, and a comment area of 8 rows and 40 columns. Again, we will keep it simplein design and process, just to get the points of OPP across. Hopefully, you will be ableto extrapolate this context into a fully operational OOP library.So let’s get back to the design of the web page form. Figure 6-1 shows the simple formfor which we will be writing the code below.Magic MethodsYou may notice that in the HTML class definition there is a function called__construct. This is a special method that you can define for each class that is triggered(executed) each time the class is instantiated. What we are doing here is establishingthe basic top of an HTML page at the same time that we are creating the object inmemory. Additionally, we are passing in a value that will be used for the page title. This__construct method is actually looked for and executed each time a class is instantiated,whether or not the code for the method is actually written. If the method is not written,it will be called, but nothing will happen visibly.The automatic method we use here (__construct) is part of a collection of predefinedmethods known as magic methods. These magic methods are all inherently definedwithin each class that you build within PHP. Even if you don’t write the code for themyourself, they still exist and the PHP parser will use them or call them as needed, Magic Methods | 65Figure 6-1. A simple form generated by object-oriented codealthough the definition will be that of the default behaviour PHP has assigned to it.Another magic method that is quite often used is the destructor method (__destruct).It is called when an object is removed from active use or when a script ends. If you wantsomething special to be performed as the object is being destroyed, create this methodwithin your class definition and add your desired code to it. You may notice in the HTML class code that there is a __construct method echoing out the content of the top of a web page (, etc.). This is merely an example of the use of one of the magic methods. Also note that this method is not returning a value, as magic methods are not permitted to do so.Be sure to look up the other magic methods on the PHP website.$thisIn the class code above, you can see a variable called $this. $this is an internal andreserved variable used within the defined objects. You don’t have to predefine it; it willbe there for your use as soon as a class in instantiated into an object. It is used forinternal references to properties, as can be seen in its referential use in relation to the66 | Chapter 6: Objects$this->tag variable. In fact, you have to use the $this-> prefix to make internal refer-ence to a class property.Objects in ActionHere is the code creating the remainder of the page. We will briefly dissect it followingthe listing: // start a table with a border, left alignment, and 30% width $webpage = $MyTable->Begin(1, left, 500) ; $webpage .= $MyTable->RowOn(); $webpage .= $MyTable->ColumnOn(); $webpage .= $MyForm->Begin() ; // proof of polymorphism $webpage .= $MyForm->InputLabel(FirstName,fname, true); $webpage .= $MyTable->ColumnOff(); $webpage .= $MyTable->ColumnOn(1,left); $webpage .= $MyForm->Input(text, fname, , , 30); $webpage .= $MyTable->ColumnOff(); $webpage ...

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

Gợi ý tài liệu liên quan: