__construct return values

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.

Moderator: General Moderators

Post Reply
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

__construct return values

Post 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
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Constructors don't return anything, so yeah.
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

viewtopic.php?t=64800&start=15 - was discussed in this thread
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post 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.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post 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.
(#10850)
User avatar
kyberfabrikken
Forum Commoner
Posts: 84
Joined: Tue Jul 20, 2004 10:27 am

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
sike
Forum Commoner
Posts: 84
Joined: Wed Aug 02, 2006 8:33 am

Post 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
User avatar
kyberfabrikken
Forum Commoner
Posts: 84
Joined: Tue Jul 20, 2004 10:27 am

Post 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.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post 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.
User avatar
kyberfabrikken
Forum Commoner
Posts: 84
Joined: Tue Jul 20, 2004 10:27 am

Post 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?
User avatar
stereofrog
Forum Contributor
Posts: 386
Joined: Mon Dec 04, 2006 6:10 am

Post 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.
Post Reply