Page 1 of 1

[SOLVED] Ouput directly from creating new object

Posted: Sun Jun 12, 2005 2:05 am
by anjanesh

Code: Select all

class class1
 {
        // Constructor
        function class1($Input)
         {
                $string = $Input;
                return $string;
         }

        // Function
        function foo($Input)
         {
                $string = $Input;
                return $string;
         }
 }
This works.

Code: Select all

$c = new class1($Input);
echo $c->foo();
But I want it like this - any way to get this to work ?

Code: Select all

echo new class1($Input);
Thanks

Posted: Sun Jun 12, 2005 8:50 am
by Chris Corbyn
The contructor isn't really a function as such. It set's your object up.

In a sense you're trying to echo an object to the page. I don't see the need for it. Is there a reason you want that?

Posted: Sun Jun 12, 2005 10:35 am
by anjanesh
Ok. Correct. It returns an object and the object # gets printed.
But what about like this ?

Code: Select all

echo new class1().foo($Input);
After removing the constructor ofcourse.

Posted: Sun Jun 12, 2005 10:50 am
by protokol

Code: Select all

<?php
echo class1::foo($input);
?>

Posted: Sun Jun 12, 2005 11:13 am
by anjanesh
Thanks for that.
Anyway I did try using scope resolution operator before but as

Code: Select all

echo new class1()::foo($Input);
which gave an error :

Parse error: syntax error, unexpected T_PAAMAYIM_NEKUDOTAYIM, expecting ',' or ';' in C:\xxx\xxx.php on line n

What the hek was that ?

Posted: Sun Jun 12, 2005 11:21 am
by John Cartwright

Code: Select all

echo new class1()::foo($Input);
should be

Code: Select all

echo new class1::foo($Input);
as you already know. "::" is the T_AAMAYIM_NEKUTOTAYIM

Posted: Sun Jun 12, 2005 11:32 am
by anjanesh
JCart - code2 gives :

Parse error: syntax error, unexpected T_STRING, expecting T_VARIABLE or '$' in C:\xxx\xxx.php on line n

protokol's solution

Code: Select all

echo class1::foo($input);
does it.

new is not allowed here :!: