btwael / mammouth

Unfancy PHP
http://mammouth.boutglay.com
MIT License
214 stars 22 forks source link

? as isset() #12

Closed CezaryDanielNowak closed 9 years ago

CezaryDanielNowak commented 10 years ago

Hey, It would be cool if we have "?" character as isset(...), like in coffeeScript;

btwael commented 10 years ago

This is a nice idea

var1?

will be compiled to:

isset($var1) && ($var1 === true)

we will work on this :)

CezaryDanielNowak commented 10 years ago

It should also work in more complex usage:

someVar = _POST['field']?['subfield'] will be compiled to: $someVar = isset($_POST['field']) ? $_POST['field']['subfield'] : null;

or

result = someFunc?() will be compiled to: $result = function_exists('someFunc') ? someFunc() : null;

CezaryDanielNowak commented 10 years ago

I think, it may conflict with bool ? true : false; statement...

btwael commented 10 years ago

I'am adding a part of this feature dc88f9e2d183363ade34c49e8497626876945600 see this example http://mammouth.wamalaka.com/#try:{{%0Aif%28var1%3F%29%0A%20%20%20echo%28var1%29%0Am%20%3D%20bool%20%3F%20true%20%3A%20false%0A}}

CezaryDanielNowak commented 10 years ago

Thanks for fast reply :)

btwael commented 10 years ago

@korpirkor, do you know any PHP method that check if variable exists (any type of variable)??

btwael commented 10 years ago

see now the example: http://mammouth.wamalaka.com/#try:{{%0Aif%28var1%3F%29%0A%20%20%20echo%28var1%29%0Am%20%3D%20bool%20%3F%20true%20%3A%20false%0A}}

CezaryDanielNowak commented 10 years ago

still some issues. try:

{{
  if(_GET['lista']?)
    echo 'list isset !'
}}
{{
  if(obj.field?)
    echo 'field isset !'
}}
{{
  field = 'someField'
  if(obj{field}?)
    echo 'list isset !'
}}
{{
  if(@field?)
    echo 'field isset !'
}}
btwael commented 10 years ago

hey @korpirkor, now you can do something like: http://mammouth.wamalaka.com/#try:{{%0Aif%28_GET[%27lista%27]%3F%29%0A%20%20%20%20echo%28%27list%20isset%20!%27%29%0Aif%28obj.field%3F%29%0A%20%20%20%20echo%28%27field%20isset%20!%27%29%0Afield%20%3D%20%27someField%27%0Aif%28obj{field}%3F%29%0A%20%20%20%20echo%28%27list%20isset%20!%27%29%20%20%0Aif%28%40field%3F%29%0A%20%20%20%20echo%28%27field%20isset%20!%27%29%0A}}

{{
if(_GET['lista']?)
    echo('list isset !')
if(obj.field?)
    echo('field isset !')
field = 'someField'
if(obj{field}?)
    echo('list isset !')  
if(@field?)
    echo('field isset !')
}}

but remember in Mammouth, functions call should be used with parentheses echo(arg) The commit changes: 78412b4896f46024a39801e9bd1bbd7b3d317e4c

CezaryDanielNowak commented 10 years ago

Cool, thanks !

regarding parentheses in echo: echo is not a standard function, it's language construct, that even in PHP can be used without ( and ); as described here: https://github.com/btwael/mammouth/issues/18 Make it work without ( and ) would make writing code much more natural and easy

btwael commented 9 years ago

@CezaryDanielNowak Finally mammouth support this one as you described in your second comment ;)