-1
A
回答
0
是的,如果該屬性不存在,會引發錯誤。檢查它存在與property_exists
$myVar = new myVar();
if((isset($myVar) && property_exists('myVar', 'myProp'))
&& $myVar->myProp !== 'error') {
}
1
這似乎是在飛行中定義屬性的情況。雖然這是可能的(合法的)使用property_exists()
,這將是多更好的實際執行的財產在類定義的所有腦幹:
Class someExample {
public $myProp = false; // now it will ALWAYS exist for any instance of someExample
}
相關問題
- 1. 如何安全地訪問對象的屬性
- 2. PHP訪問對象的屬性
- 3. 訪問具有屬性的php對象
- 4. 訪問PHP對象的屬性
- 5. 訪問php對象的多個屬性
- 6. Grails在訪問域對象屬性時警告安全問題
- 7. 對象的訪問屬性
- 8. 訪問對象屬性中
- 9. 訪問PHP 7對象屬性
- 10. 無法訪問PHP對象屬性
- 11. 訪問數組中對象的屬性
- 12. 訪問NSSet中對象的屬性
- 13. 陣列中的訪問對象屬性
- 14. 訪問對象數組中的屬性
- 15. 訪問ArrayList中對象的屬性
- 16. 允許不安全地訪問security.yml中的Symfony對象操作
- 17. 安全地限制對FileProvider的訪問
- 18. LuaInterface:訪問對象屬性
- 19. 訪問AppDelegate對象屬性
- 20. 訪問對象屬性
- 21. Javascript - 訪問對象屬性
- 22. 訪問對象屬性
- 23. jQuery - 訪問對象屬性
- 24. 訪問對象屬性[]
- 25. 在JavaScript中的對象中訪問對象內部的屬性
- 26. 訪問的form_for對象的屬性
- 27. 訪問對象的數組中的對象的屬性
- 28. 訪問`contentWindow`屬性是否安全?
- 29. 從屬性在PHP中的子對象訪問父方法?
- 30. 動態訪問PHP中的對象屬性數組元素
如果它不存在,這可能會導致錯誤.. 。但是,當然,你在使用它之前檢查它是否存在,對吧? – 2014-10-16 13:08:38
好的,試着讓它更清晰 – Cotten 2014-10-16 13:17:01
對我來說,在編輯之前是同樣的問題。 – 2014-10-16 13:20:20