SitePoint Sponsor

User Tag List

Page 3 of 4 FirstFirst 1234 LastLast
Results 51 to 75 of 76

Thread: PHP Syntax - how would you change it?

  1. #51
    One website at a time mmj's Avatar
    Join Date
    Feb 2001
    Location
    Melbourne Australia
    Posts
    6,282
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here's what I'd change in PHP if could:



    1. Make it mandatory to declare a variable before initialising it kinda like Javascript:

    Code:
    var $newvar = 1;
    This will prevent a lot of mistakes that could be hard to debug or lead to security holes.

    2. Remove extract(), compact(), register_globals, the global keyword (maybe keep $_GLOBALS).

    3. Think about object literals. Ideally they would create new objects of class stdClass or something.

    Code:
    $myobj = object(x => 4, y => 2);
    The actual syntax I used is debatable; to emulate Javascript you could use {x:4, y:2} but the above seemed more consistent with other parts of PHP such as array().

    PS. I don't like many of the previous suggestions. If there's already a way to do it in PHP then why not leave it as is, for example $this->something is perfectly fine and it's not as if #something saves any actual lines of code (in this instance, I'd say it makes it less intuitive as well).
    [mmj] My magic jigsaw
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    The Bit Depth Blog · Twitter · Contact me
    Neon Javascript Framework Jokes

  2. #52
    @php.net Salathe's Avatar
    Join Date
    Dec 2004
    Location
    Edinburgh
    Posts
    1,360
    Mentioned
    29 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by mmj View Post
    If there's already a way to do it in PHP then why not leave it as is
    Then why not $myobj = (object) array('x' => 4, 'y' => 2); ?
    Salathe
    Software Developer and PHP Documentation Team.

  3. #53
    SitePoint Enthusiast
    Join Date
    Oct 2005
    Posts
    33
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    $v = 0 || 0 || 'A';

    $v would contain 'A'. (not TRUE)

    I'd add unless(){ ... }

    if(! blah) { } // <--- this stinks.

    I'd add post-conditionals and get rid of single line conditionals.

    if(true)
    do_something(); // <-- this stinks

    do_something() if(true); // <-- this is more readable for 1 liners.

    inline, *LEXICALLY SCOPED* finctions.

    function bark(){
    $a = "Apple";
    $accum = array(A => '');
    $ptr = &$accum['A']; // or \$accum['A'] I don't care.. long as $ptr works
    $v = function(){
    print $a;
    *$ptr .= "Appended";
    }
    $v->() // prints "Apple"
    print $accum['A']; // Now contains "Appended"
    }

    Sane, predictable reference handling with lexically scoped functions is extremely useful for callback stuff, like parsing XML. True, it's easy to hang yourself, but it's a lot easier than trying to keep track of the hash index you were using (plus it ought to be faster, only need the hash lookup once)

    On the subject of references... everything would be passed by reference, all the time. If you want a copy, you have to explicitly do that. (maybe php5 has this, I don't know... often, when I've tried to use php's references I get segfaults)

    I'd *GET RID OF ALL THESE BUILT IN FUNCTIONS*

    For example, mysql_ would go bye-bye, as would 90% of the other functions.

    I'd get rid of $_GET, $_POST and friends, replace with some sort of Request() object that allowed you to specify things (like whether you want \' escaped each time or if you like using ; to separate variables) at the time of construction, instead of just having them "appear". No more foo.php?v[A]=bark unless you want this. (indeed, who says input is always urlencoded?)

    Some sort of super-static area, I realize the archetecture doesn't support it, but in some cases, like with other languages and FastCGI or Servlets, it's nice to create an object and re-use it for multiple requests.

    And my #1 PHP peeve: BYE-BYE HOST QUIRKS!!!

    No more "compile time disabled" crap, nor host-centric "settings". If a function is in the docs, you should be able to rely on it always working. No more having to:

    if(this_wacky_host_has_blah_set()) { ... work around quack host .. }

    PHP should work the same, on all hosts, with very, very few exceptions. When the first 200 lines of your program are tests to work around different "configurations", something is seriously wrong.

    I want an standard installation pr. user, some place a web application can store code, settings and data. OUTSIDE OF WEB SPACE.

    Sort of like WEB-INF/

    There should be some mechanism for users to adjust ini files in WEB-INF/conf Ideally with a template, so when you write an installer, you can query for something that is likely to be their mysql/postgresql/website/blah settings and present it to them as defaults.

    It should be possible to install libraries in this directory w/out a huge hassle.

    Most of all, I don't want stuff being placed in the document root unless it's explicitly there for web browsers to access.

    .. and probably lots more things ...

  4. #54
    SitePoint Guru TomB's Avatar
    Join Date
    Oct 2005
    Location
    Milton Keynes, UK
    Posts
    960
    Mentioned
    8 Post(s)
    Tagged
    2 Thread(s)
    oh, that reminds me: infix functions!

    PHP Code:
    infix function add($a$b) {
    return 
    $a $b;
    }

    echo 
    1 add 2//prints 3. 

  5. #55
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,109
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by TomB View Post
    oh, that reminds me: infix functions!

    PHP Code:
    infix function add($a$b) {
    return 
    $a $b;
    }

    echo 
    1 add 2//prints 3. 
    What would be the benefit though Tom, just syntactic sugar?
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  6. #56
    SitePoint Guru TomB's Avatar
    Join Date
    Oct 2005
    Location
    Milton Keynes, UK
    Posts
    960
    Mentioned
    8 Post(s)
    Tagged
    2 Thread(s)
    Consider this:

    PHP Code:
    class MyClass {
      public 
    $total;
    }

    $obj1 = new MyClass;
    $obj1->total 1;

    $obj2 = new MyClass;
    $obj2->total 2;

    infix function +(MyClass $aMyClass $b) {
     return 
    $a->total $b->total;
    }

    echo 
    $obj1 $obj2
    Of course you'd also need method overloading for it to work really well.

    One of my favourite things about Eiffel

  7. #57
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,109
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Very interesting! Thanks again.
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  8. #58
    ¬.¬ shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    8,990
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by geniegate View Post
    $v = 0 || 0 || 'A';

    $v would contain 'A'. (not TRUE)
    Actually 'A' is TRUE. It evaluates to a non-empty value.

    When converting to boolean , the following values are considered FALSE:

    I'd *GET RID OF ALL THESE BUILT IN FUNCTIONS*

    For example, mysql_ would go bye-bye, as would 90&#37; of the other functions.
    Disable or remove the MySQL Extension. That is where the mysql_* functions reside. Along with most others.
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  9. #59
    Do. Or do not. There is no try silver trophy
    SitePoint Award Recipient ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    8,346
    Mentioned
    87 Post(s)
    Tagged
    2 Thread(s)
    Quote Originally Posted by logic_earth View Post
    Actually 'A' is TRUE. It evaluates to a non-empty value.
    I think what geniegate meant is:

    PHP Code:
    $a || 'A';
    var_dump($a); // output: bool (true) 
    While he would want it to output string(1) "A", like javascript does.

    Code javascript:
    var a = 0 || 'A';
    alert(a); // alerts 'A'

    Rémon - Hosting Advisor

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  10. #60
    SitePoint Enthusiast
    Join Date
    Oct 2005
    Posts
    33
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by logic_earth View Post
    Disable or remove the MySQL Extension. That is where the mysql_* functions reside. Along with most others.
    And no doubt, some hosts have done this, other hosts haven't.

    When a hosting provider says they "have php" it's really quite meaningless, their PHP is very likely not the same PHP as yours or the guys next door.

    Hence the 200+ lines of code you see in php applications that test for this...

    For kicks (and saddly, true in some respects!):
    http://uncyclopedia.wikia.com/wiki/PHP

    There should really only be a handful of functions, things like "array" are redundant.

  11. #61
    SitePoint Guru TomB's Avatar
    Join Date
    Oct 2005
    Location
    Milton Keynes, UK
    Posts
    960
    Mentioned
    8 Post(s)
    Tagged
    2 Thread(s)
    Wait so inbuilt database connectivity is bad?

    Extensions are bad?

  12. #62
    @php.net Salathe's Avatar
    Join Date
    Dec 2004
    Location
    Edinburgh
    Posts
    1,360
    Mentioned
    29 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by geniegate View Post
    $v = 0 || 0 || 'A';
    PHP Code:
    $a ?: ?: 'A'
    Salathe
    Software Developer and PHP Documentation Team.

  13. #63
    I solve practical problems. bronze trophy
    Michael Morris's Avatar
    Join Date
    Jan 2008
    Location
    Knoxville TN
    Posts
    1,902
    Mentioned
    32 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Wardrop View Post
    I can certainly see your logic there, but such a change would do more harm than good. The harm is increased complexity. I'm not sure about you, but learning is not fun. If I have to get my hands dirty with a new language, API or environment, it has to be as simple as possible, so I can quickly jump in and get to work. The best languages, frameworks and API's are based on the less is more approach.
    Beginning coders have trouble with the concept of datatypes. Expert programmers prefer to use them and avoid loose typing when feasible. There's no clean way around that fact. Hence offer both approaches as I outlined. No one is going to hold a gun to your head and demand you use tolerant or strict vars - and the language would continue working just fine without their use. When you are ready to deal with strict data casting the feature would be there waiting for you. That's hardly in the way and hardly an increase in complexity.



    Quote Originally Posted by mmj View Post
    Here's what I'd change in PHP if could:

    1. Make it mandatory to declare a variable before initialising it kinda like Javascript:
    You don't program Javascript very often do you? It isn't mandatory to declare a variable before using it in javascript. Advised, but hardly mandatory.

    The danger of not declaring the variable is largely that of scope. When a variable is referenced the interpreter checks each scope up the chain for the object until it finds the variable or hits the window object. If it hasn't found it when it hits the window object it creates the variable in the local scope and proceeds.

    2. Remove extract(), compact(), register_globals, the global keyword (maybe keep $_GLOBALS).
    No. Hell no. Register globals is gone in PHP 6 and that is a good thing - along with it say buh bye to safe mode and magic quotes.

    Compact is incredibly useful in debugging - so no, removing that is a very stupid suggestion.

    Extract is more convenience than necessary, but helps keep code readable. I use it to very powerful effect in my template engine.

    The global keyword is necessary as part of function scoping. Yes, it can be misused to create hidden dependencies in a library or framework, and yes it is bad practice to have a function import a var into scope with global and then change it's value rather than return it, but it has it's place in the language.

    Just like eval() does for better or worse.

    And it's $GLOBALS, not $_GLOBALS.

  14. #64
    I solve practical problems. bronze trophy
    Michael Morris's Avatar
    Join Date
    Jan 2008
    Location
    Knoxville TN
    Posts
    1,902
    Mentioned
    32 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by TomB View Post
    Wait so inbuilt database connectivity is bad?

    Extensions are bad?
    Heh heh.. Extensions are great. Extensions you don't need are indeed bad.

    Again, I'd like to see the existing functionality cordoned off into a legacy namespace. By default it would be included into the project but you can turn it off in PHP.ini, or in the apache .htaccess or .httpd.config directive files.

    If you turn off the legacy namespace you are left with the core namespace which is only the essential functions. Database connectivity would require importing the namespace of the DB you are actually going to use into your project.

    I would imagine that this would help the interpreter parse the scripts faster since the number of functions and constants to consider during pattern matches would be considerably reduced.

  15. #65
    SitePoint Wizard bronze trophy Immerse's Avatar
    Join Date
    Mar 2006
    Location
    Netherlands
    Posts
    1,661
    Mentioned
    6 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by geniegate View Post
    For kicks (and saddly, true in some respects!):
    http://uncyclopedia.wikia.com/wiki/PHP
    Best thing on that page:

    PHP's coding style pulls common elements from C++, Java, PERL, Python, BASIC, Assembly, Dragonspeak, and Microsoft Office Excel.


  16. #66
    SitePoint Enthusiast
    Join Date
    Oct 2005
    Posts
    33
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Michael Morris View Post
    Again, I'd like to see the existing functionality cordoned off into a legacy namespace. By default it would be included into the project but you can turn it off in PHP.ini, or in the apache .htaccess or .httpd.config directive files.
    Only problem with that is, you're back with "on some hosts X, on other hosts Y". Not that I have anything better to offer, save for just abandoning any hope of backwards compatibility.

    I suppose this is the core problem with PHP, it was originally designed as a kind of templating system for C libraries, and in that particular context, it does a really good job of it.

    As an actual language, it's horrible, but as a templating system, it's wonderful! so wonderful in fact, that people wanted to use it for everything, and thats how it came to be the awful mess it is today.

    Quote Originally Posted by Michael Morris View Post
    If you turn off the legacy namespace you are left with the core namespace which is only the essential functions. Database connectivity would require importing the namespace of the DB you are actually going to use into your project.
    I like this idea!

    Especially if there were a set of standard modules that you could rely on.

    One of the disadvantages of being embedded into the web server is that you sort of pull all these libraries along for the ride, having dozens of libraries linked into PHP is a serious liability, but, unless it's a separate process, I can't really see a good way around it.

    Call me silly, but I actually like much of what perl's DBI has. DSN's are kind of "weird", but they do allow much greater flexibility in other parts of the code, for vanilla SQL stuff, you don't have to care which database it is.

    I also really like placeholders, they can dramatically decrease SQL injection bugs... but thats more of a "what I hate about mysql" essay.. another wonderful tool that was expanded well beyond what it had originally set out to do.

    There was once a time, when I thought "lpr <file.mp3" would be a neat thing to do. After seeing what happened with these templating systems and tiny simple databases.. I now see the error of my thinking.

  17. #67
    One website at a time mmj's Avatar
    Join Date
    Feb 2001
    Location
    Melbourne Australia
    Posts
    6,282
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Salathe View Post
    Then why not $myobj = (object) array('x' => 4, 'y' => 2); ?
    Good point, you got me there
    [mmj] My magic jigsaw
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    The Bit Depth Blog · Twitter · Contact me
    Neon Javascript Framework Jokes

  18. #68
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,004
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by joebert View Post
    I wish "->" would be replaced with one character, one that doesn't require the shift key.
    That's it what I would like to see changed at first hand

    Apart from that, there might be lots of things to be 'would be better...' not essentially

  19. #69
    @php.net Salathe's Avatar
    Join Date
    Dec 2004
    Location
    Edinburgh
    Posts
    1,360
    Mentioned
    29 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by joebert
    I wish "->" would be replaced with one character, one that doesn't require the shift key.
    Which character would you prefer from (on my keyboard):
    1234567890 `-=[];'#\,./
    acbdefghijklmnopqrstuvwxyz
    Salathe
    Software Developer and PHP Documentation Team.

  20. #70
    Non-Member bronze trophy
    Join Date
    Nov 2009
    Location
    Keene, NH
    Posts
    3,760
    Mentioned
    23 Post(s)
    Tagged
    0 Thread(s)
    While I'm sitting here raging "OH NOES, NOT SHIFT!!!"

  21. #71
    SitePoint Zealot GOPalmer's Avatar
    Join Date
    Jan 2009
    Location
    Wiltshire, UK
    Posts
    125
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Why change anything? I mean, nothing is so bad we can't get along with it. I don't think it would help much anyway. For example changing -> to . notation may seem like a great idea, but what about all the reusable code libraries we've been building up, would we have to change all of that too? Or would we end up still using -> to keep our code-bases consistent?

    Rather than trying to make PHP like other langauges, why not just embrace its PHPnes.

  22. #72
    I solve practical problems. bronze trophy
    Michael Morris's Avatar
    Join Date
    Jan 2008
    Location
    Knoxville TN
    Posts
    1,902
    Mentioned
    32 Post(s)
    Tagged
    0 Thread(s)
    That's why one change is a wish and never going to be practical, and the other is something I'd like to see. Changing to dot syntax is never going to be practical.

    On the other hand, a legacy namespace arrangement could be installed.

  23. #73
    SitePoint Enthusiast
    Join Date
    Jan 2005
    Location
    UK
    Posts
    97
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Nothing, wouldn't want to upset almost 10 years of muscle memory.

    Changing -> to something else could only happen at a full version, say 7 to make it easy for libraries etc. As Go says, realistically though there would have to be some other pretty impressive changes for Businesses to choose to rewrite all their legacy code.

  24. #74
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,049
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by G3D View Post
    Nothing, wouldn't want to upset almost 10 years of muscle memory.

    Changing -> to something else could only happen at a full version, say 7 to make it easy for libraries etc. As Go says, realistically though there would have to be some other pretty impressive changes for Businesses to choose to rewrite all their legacy code.
    Actually, code converting applications would be very simple to write, so changing legacy code isn't that difficult.

    However, if you're going to upgrade your PHP major version, you should really be changing your code too. PHP4 code is better suited on a PHP 4 server, etc. I'm talking about future developments.

    I stick to my belief that a 'fork' of PHP should be started; it is open-source after all. Maybe call it PHPi? This would involve above suggestions such as a completely rewritten library, scalars existing as objects, a better concatenation operator (I'd prefer the same as with numbers - "hello" + "world") and a different object operator (remember that -> isn't as easy on some keyboards as it is with standard US/UK) I'd also like 'infix' methods (Or as I'm familiar with them, operator overloaders).

    That way it doesn't interfere with most programmers who want PHP to stay the same, but offers a platform more satisfying for those who wish for something different.
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  25. #75
    From space with love SitePoint Award Recipient SpacePhoenix's Avatar
    Join Date
    May 2007
    Location
    Poole, UK
    Posts
    4,270
    Mentioned
    54 Post(s)
    Tagged
    0 Thread(s)
    Jake, what's your thoughts on the $_REQUEST superglobal, do you think that it should be depreciated and why?
    Community Team Advisor
    Forum Guidelines: Posting FAQ Signatures FAQ Self Promotion FAQ
    Help the Mods: What's Fluff? Report Fluff/Spam to a Moderator

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •