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
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php