TAILIEUCHUNG - Object-Oriented Programming with PHP 5 phần 3

Chúng tôi thảo luận trong phần trước bằng văn bản phương pháp accessor cho một số tài sản sẽ là một cơn ác mộng thực sự. Để tránh sự nhàm chán, bạn có thể sử dụng các phương pháp ma thuật. Quá trình này được gọi là tài sản quá tải. PHP5 đã giới thiệu một số phương pháp kỳ diệu trong các lớp học | Kick-Starting OOP Using Magic Methods to Set Get Class Properties We discussed in the previous section that writing accessor method for a number of properties will be a real nightmare. To avoid that boredom you can use magic methods. This process is called property overloading. PHP5 introduced some magic methods in classes to reduce the pain of OOP in some cases. Two of those magic methods are introduced to set and get dynamic property values in a class. These two magic methods are named as_get and_set . Let us see how to use them class Student private properties array function __get property return this- properties property function __set property value this- properties property AutoSet property as . value Now let us see the code in action. Use the class above with the following script st new Student st- name Afif st- roll 16 echo st- name. n echo st- roll When you execute the preceding code PHP recognizes immediately that no property named name or roll exists in the class. Since the named property doesn t exist the __set method is called which then assigns the value to the newly-created property of the class allowing you to see the following output AutoSet name as Afif AutoSet roll as 16 ----------------------------------- 40 --------------------------------- Chapter 2 Seems quite interesting huh Using magic methods you still have full control over setting and retrieving property values in classes. However you have one limitation if you use magic methods. While using reflection API you can t investigate class properties we will discuss about reflection API in a later chapter . Moreover your class lost the readability and maintainability quite a lot. Why See the code of previous Student class and new Student class and you will understand that for yourself. Magic Methods for Overloading Class Methods Like overloading and using the accessor methods there are magic methods to overload any method call in a class. If you are still not familiar with .

TỪ KHÓA LIÊN QUAN
TAILIEUCHUNG - Chia sẻ tài liệu không giới hạn
Địa chỉ : 444 Hoang Hoa Tham, Hanoi, Viet Nam
Website : tailieuchung.com
Email : tailieuchung20@gmail.com
Tailieuchung.com là thư viện tài liệu trực tuyến, nơi chia sẽ trao đổi hàng triệu tài liệu như luận văn đồ án, sách, giáo trình, đề thi.
Chúng tôi không chịu trách nhiệm liên quan đến các vấn đề bản quyền nội dung tài liệu được thành viên tự nguyện đăng tải lên, nếu phát hiện thấy tài liệu xấu hoặc tài liệu có bản quyền xin hãy email cho chúng tôi.
Đã 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.