php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #53378 ( new Class() ) -> method() throws a PHP parse error
Submitted: 2010-11-22 13:41 UTC Modified: 2010-11-23 10:32 UTC
From: marco dot weber at uni-trier dot de Assigned:
Status: Duplicate Package: Scripting Engine problem
PHP Version: 5.3.3 OS: ANY
Private report: No CVE-ID: None
 [2010-11-22 13:41 UTC] marco dot weber at uni-trier dot de
Description:
------------
In Java or C# it is allowed to do ( new Class() ) -> method() calls, why not in PHP?

This is very useful for more complex commands, f.e.:
     $collection = new QueryableCollection($employees);
     $collection->setAsName('c1');
     $collection->where(new OrCondition(
                    new ColumnEquals( ( new Column('c1.job') ) ->subString(0, 5)->toLower(), 'admin'),
                    new AndCondition(
                      new ColumnBiggerThan('c1.age', 10),
                      new ColumnLowerThan('c1.age', 20),
                    )
                  ))
                ->sortByDesc( ( new Column('c1.job') ) ->toLower())
                ->sortByAsc('c1.nachname')
                ->sortByAsc('c1.vorname')
                ->select('c1.*');


Test script:
---------------
<?php

class MyString {
        protected $string='';
        public function __construct($string) {
                if(is_string($string)) $this->string=$string;
        }
        public function length() {
                return strlen($this->string);
        }
}

echo("This string has a length of: " . (new MyString('teststring'))->length() ."\n");

/* instead you have to do it like that :(
$ms=new MyString('teststring'); 
echo("This string has a length of: " . $ms->length() ."\n");
*/
?>

Expected result:
----------------
PHP Parse error:  syntax error, unexpected T_OBJECT_OPERATOR in ...

Actual result:
--------------
No Syntax error. PHP should just create the object and execute its Method.

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2010-11-22 16:41 UTC] cataphract@php.net
-Type: Bug +Type: Feature/Change Request
 [2010-11-23 10:32 UTC] aharvey@php.net
-Status: Open +Status: Duplicate
 [2010-11-23 10:32 UTC] aharvey@php.net
Duplicate of numerous other requests, the oldest I can find being 
request #34502. Patches welcome, I suspect.
 
PHP Copyright © 2001-2017 The PHP Group
All rights reserved.
Last updated: Sun Nov 19 01:31:42 2017 UTC