A Lesson In Static Methods And Late Static Binding

Until last week, I had never experienced what must have been incredibly frustrating to most developers: the fact that the self keyword in PHP refers to the class it is located in, and not necessarily a class that extends it. I personally ran into this problem when trying to extend Zend_Auth. Being a singleton, the constructor in Zend_Auth is protected, and the static method Zend_Auth::getInstance() instantiates itself. The problem is, when extended, My_Auth::getInstance() still returns an instance of Zend_Auth. The solution was to duplicate the static method in my My_Auth class, which worked properly. For example:


Monday, April 12th, 2010 @ 7:00 am | Comment (4) | Categories: PHP 5, Object-Oriented Development, Zend Framework, Technology
Tags: , , , ,

Copyright © 2024 by Brandon Savage. All rights reserved.