Page 1 of 1

__construct return values

Posted: Mon Jun 18, 2007 10:25 pm
by Benjamin
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.

Code: Select all

<pre>
<?php
echo "MEM_USAGE: " . number_format(memory_get_usage()) . "<br />";

class test
{
    public function __construct()
    {
        global $test;
        if (is_object($test)) { return $test; }
    }
}

$test = new test();

echo "loaded 1: " . var_dump($test) . "<br />";
echo "MEM_USAGE: " . number_format(memory_get_usage()) . "<br />";

$test = new test();

echo "loaded 2: " . var_dump($test) . "<br />";
echo "MEM_USAGE: " . number_format(memory_get_usage()) . "<br />";

$test = new test();

echo "loaded 3: " . var_dump($test) . "<br />";
echo "MEM_USAGE: " . number_format(memory_get_usage()) . "<br />";

$test = new test();

echo "loaded 4: " . var_dump($test) . "<br />";
echo "MEM_USAGE: " . number_format(memory_get_usage()) . "<br />";

$test = new test();

echo "loaded 5: " . var_dump($test) . "<br />";
echo "MEM_USAGE: " . number_format(memory_get_usage()) . "<br />";

$test = new test();

echo "loaded 6: " . var_dump($test) . "<br />";
echo "MEM_USAGE: " . number_format(memory_get_usage()) . "<br />";

echo "PEAK_MEM_USAGE: " . number_format(memory_get_peak_usage()) . "<br />";
Outputs..
MEM_USAGE: 56,580
object(test)#1 (0) {
}
loaded 1:
MEM_USAGE: 56,892
object(test)#2 (0) {
}
loaded 2:
MEM_USAGE: 56,980
object(test)#1 (0) {
}
loaded 3:
MEM_USAGE: 56,980
object(test)#2 (0) {
}
loaded 4:
MEM_USAGE: 56,980
object(test)#1 (0) {
}
loaded 5:
MEM_USAGE: 56,980
object(test)#2 (0) {
}
loaded 6:
MEM_USAGE: 56,980
PEAK_MEM_USAGE: 80,628

Posted: Mon Jun 18, 2007 10:30 pm
by feyd
Constructors don't return anything, so yeah.

Posted: Mon Jun 18, 2007 10:31 pm
by Luke
viewtopic.php?t=64800&start=15 - was discussed in this thread

Posted: Mon Jun 18, 2007 10:39 pm
by Benjamin
Are all OOP languages like that?

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.

Posted: Mon Jun 18, 2007 10:41 pm
by feyd
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.

Posted: Tue Jun 19, 2007 4:33 am
by Jenk
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.

Posted: Tue Jun 19, 2007 4:48 am
by Christopher
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.

Posted: Tue Jun 19, 2007 8:37 am
by kyberfabrikken
feyd wrote:All the languages I am quite familiar with do the exact same thing. Constructors are a special function/method that do not return anything.
You can do it in PHP4.

Javascript also allows you to return an object from the constructor. But Javascript is a pretty special case, because it isn't class based.

Posted: Tue Jun 19, 2007 8:45 am
by feyd
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.
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.

Posted: Tue Jun 19, 2007 8:47 am
by sike
arborint wrote:
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

Posted: Tue Jun 19, 2007 8:49 am
by kyberfabrikken
sike wrote:
arborint wrote:
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.

Posted: Tue Jun 19, 2007 10:45 am
by Benjamin
The reason why I tested PHP to see if this would work is because I did not want to use a Factory or Singleton.

Posted: Tue Jun 19, 2007 3:05 pm
by kyberfabrikken
astions wrote:The reason why I tested PHP to see if this would work is because I did not want to use a Factory or Singleton.
That begs the question ... Why?

Posted: Tue Jun 19, 2007 3:42 pm
by stereofrog
astions wrote:Are all OOP languages like that?
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.