tailieunhanh - Tương tác giữa PHP và jQuery - part 10

Để sử dụng các phương pháp này, gọi cho họ cũng giống như chức năng thường xuyên, nhưng tham khảo, đầu tiên các đối tượng mà họ thuộc về. Đọc các tài sản từ MyClass, thay đổi giá trị của nó, và đọc nó ra một lần nữa bằng cách làm những sửa đổi được in đậm: | CHAPTER 3 OBJECT-ORIENTED PROGRAMMING Note OOP allows objects to reference themselves using this. When working within a method use this in the same way you would use the object name outside the class. To use these methods call them just like regular functions but first reference the object to which they belong. Read the property from MyClass change its value and read it out again by making the modifications shown in bold php class MyClass public prop1 I m a class property public function setProperty newval this- prop1 newval public function getProperty return this- prop1 . br obj new MyClass echo obj- getProperty Get the property value obj- setProperty I m a new property value Set a new one echo obj- getProperty Read it out again to show the change Reload your browser and you ll see the following I m a class property I m a new property value The power of OOP becomes apparent when you re using multiple instances of the same class. Add an additional instance of MyClass into the mix and start setting and getting properties 91 CHAPTER 3 OBJECT-ORIENTED PROGRAMMING php class MyClass public prop1 I m a class property public function setProperty newval this- prop1 newval public function getProperty return this- prop1 . br Create two objects obj new MyClass obj2 new MyClass Get the value of prop1 from both objects echo obj- getProperty echo obj2- getProperty Set new values for both objects obj- setProperty I m a new property value obj2- setProperty I belong to the second instance Output both objects prop1 value echo obj- getProperty echo obj2- getProperty When you load the results in your browser they read as follows I m a class property I m a class property I m a new property value I belong to the second instance As you can see OOP keeps objects as separate entities which makes for easy separation of different pieces of code into small related bundles. 92 CHAPTER 3 OBJECT-ORIENTED PROGRAMMING To make the use of objects easier PHP also provides a number of magic methods or

TỪ KHÓA LIÊN QUAN
crossorigin="anonymous">
Đã 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.