2016年5月3日 星期二

PHP class Property 和 method 名稱相同

PHP 的 class,屬性和方法名稱可以相同。
至於使用時,是存取屬性或調用方法,則視寫法區別,例如 $obj->bar 或 $obj->bar()

官網說明:
Class properties and methods live in separate "namespaces", so it is possible to have a property and a method with the same name. Reffering to both a property and a method has the same notation, and whether a property will be accessed or a method will be called, solely depends on the context, i.e. whether the usage is a variable access or a function call.


參考:
php - member variable and member function have the same name - Stack Overflow
PHP: The Basics - Manual (Classes and Objects)


沒有留言:

張貼留言