Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.
Was just playing around. It appears that you cannot return an object with a __construct()? This would eliminate the need to create static vars and private constructs in singletons.
All the languages I am quite familiar with do the exact same thing. Constructors are a special function/method that do not return anything. In other languages there are other special methods, too.
Smalltalk let's you return any value from the constructor (method #initialize), though you can also override the equivalent of PHP's "new" keyword. Infact, there isn't a single thing you can't change/override.
astions wrote:I would like to be able to override the object returned by new. It's not a necessity or anything, it would just come in handy once in a while.
I don't think so... Unless you're referring to being able to call the constructor outside of the "new" operator, in which case it will act as a normal method, of course.
kyberfabrikken wrote:Javascript also allows you to return an object from the constructor. But Javascript is a pretty special case, because it isn't class based.
True constructors, I don't think so. There are a bunch of ways to create objects in Javascript however. Many are actually factories.
astions wrote:I would like to be able to override the object returned by new. It's not a necessity or anything, it would just come in handy once in a while.
astions wrote:I would like to be able to override the object returned by new. It's not a necessity or anything, it would just come in handy once in a while.
That would be a Factory.
or a singleton
I'd say a singleton is a kind of factory.
feyd wrote:
kyberfabrikken wrote:Javascript also allows you to return an object from the constructor. But Javascript is a pretty special case, because it isn't class based.
True constructors, I don't think so. There are a bunch of ways to create objects in Javascript however. Many are actually factories.
I'd say a constructor is a kind of factory. In Javascript especially.
feyd wrote:
kyberfabrikken wrote:You can do it in PHP4.
I don't think so... Unless you're referring to being able to call the constructor outside of the "new" operator, in which case it will act as a normal method, of course.
I may be remembering it wrong -- It's not a feature I ever made use of, for obvious reasons.
Languages that allow control over how objects are created, usually do that by redefining "new". For example, in ruby "new" is a method like any other and can return whatever you want. C++ allows any class to have its own custom "new" operator.
astions wrote:
I would like to be able to override the object returned by new. It's not a necessity or anything, it would just come in handy once in a while.
I played with this feature a bit in pihipi and came to conclusion it actually raises more problems than it seems to solve, at least for two reasons: inconsistency and poor readability.