Re: new keyword combined with other things...

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



I know how to do it in other ways.
I was just wondering why the simple new Object() -> method won't work. new
operator has precedence over.......
That must be the problem.  -> is not an operator. Is not in this list:
http://php.net/manual/en/language.operators.precedence.php
That must be done. -> should be an operator!

Alex



On Tue, Dec 7, 2010 at 10:49 PM, Jim Lucas <lists@xxxxxxxxx> wrote:

> On 12/7/2010 7:40 AM, Alexandru Patranescu wrote:
> > In many other languages this will work:
> >
> > *$result = new Object() -> method();*
> >
> > But in php, it fails at parsing.
> > I've tried with parenthesis around new but nothing. Anyhow, as I saw
> later,
> > *new* operator has precedence over others so this couldn't be a solution.
> > I know a static function can be defined and the above statement can
> > transform into
> >
> > *$result = Object::getNewInstance() -> method()*
> >
> > but is there any way to write it directly? and if not, why isn't this
> > implemented yet and when will it be?
> >
> > best regards,
> > Patranescu Alexandru
> >
>
>
> Here is what I do if I want to make it a one liner...
>
> <?php
>
> function myNew($obj='stdClass') {
>        return new $obj();
> }
>
> class CustomClass {
>        function PrintString($str='Something')
>        {
>                print($str);
>        }
>        function ReturnString($str='Something')
>        {
>                return $str;
>        }
> }
>
>
> myNew('CustomClass')->PrintString();
>
> echo myNew('CustomClass')->ReturnString('Something Else');
>
> $var = myNew('CustomClass')->ReturnString('And again...');
>
> echo $var;
>
> ?>
>
> I also use the following if I want to use the Singleton method of getting
> my data.
>
> <?php
>
> class myClass {
>        static $_instance;
>        static function run($DefaultValues=null)
>        {
>                if(self::$_instance === null)
>                {
>                        //First and only construction.
>                        self::$_instance = new self($DefaultValues);
>                }
>                return self::$_instance;
>        }
>        function PrintString($str='Default Value')
>        {
>                print $str;
>        }
>        function ReturnString($str='Something')
>        {
>                return $str;
>        }
> }
>
> myClass::run()->PrintString();
>
> echo myClass::run()->ReturnString();
>
> $var = myClass::run()->ReturnString();
>
> echo $var;
>
> ?>
>
> YMMV
>

[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux