[SOLVED] Ouput directly from creating new object

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

[SOLVED] Ouput directly from creating new object

Post 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
Last edited by anjanesh on Sun Jun 12, 2005 11:14 am, edited 1 time in total.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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?
User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

Post 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.
User avatar
protokol
Forum Contributor
Posts: 353
Joined: Fri Jun 21, 2002 7:00 pm
Location: Cleveland, OH
Contact:

Post by protokol »

Code: Select all

<?php
echo class1::foo($input);
?>
User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

Post 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 ?
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post 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
User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

Post 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 :!:
Post Reply