tailieunhanh - A Programmer’s Introduction to PHP 4.0 phần 4

Chú ý Đây là một quy ước chung mà OO các lớp học bắt đầu với một bức thư vốn, trong khi phương pháp bắt đầu bằng chữ thường với chữ hoa tách mỗi từ một tên chức năng multiword. Tất nhiên, bạn có thể sử dụng danh mục bất cứ điều gì bạn cảm thấy thoải mái nhất, chỉ cần chắc chắn để chọn một tiêu chuẩn và gắn bó với nó. | Chapter 6 NOTE It is a general convention that OO classes begin with a capital letter while methods start in lowercase with uppercase separating each word from a multiword function name. Ofcourse you can use whatever nomenclature you feel most comfortable with just be sure to choose a standard and stick with it. One main use of methods is to manipulate the various attributes constituting the class. However these attributes are referenced in the methods using a special variable called this. Consider the following example demonstrating the use of this syntax class Webpage var bgcolor function setBgColor color this- bgcolor color function getBgColor return this- bgcolor The this variable is referring to the particular object making use of the method. Because there can be many object instances of a particular class this is a means of referring to the attribute belonging to the calling or this object. Furthermore there are two points regarding this newly introduced syntax worth mentioning The attribute being referenced in the method does not have to be passed in as would a functional input parameter. A dollar sign precedes only the this variable and not the attribute itself as would be the case with a normal variable. 124 Object-Oriented PHP Creating and Working with Objects An object is created using the new operator. An object based on the class Webpage can be instantiated as follows some_page new Webpage The new object some_page now has its own set of attributes and methods specified in the class Webpage. The attribute bgcolor corresponding to this specific object can then be assigned or changed via the predefined method setBgColor some_page- setBgColor black Keep in mind that PHP also allows you to retrieve the value by explicitly calling the attribute along with the object name some_page- bgcolor However this second method of retrieval defeats the purpose of encapsulation and you should never retrieve a value this way when working with OOP To better understand why

TỪ KHÓA LIÊN QUAN