#1
  1. A Change of Season
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2004
    Location
    Nobbies beach, Gold Coast. It's beautiful.
    Posts
    2,577
    Rep Power
    171

    Sending multiple argments to a function that expects only 1 argument


    Probably very basic but I'm surprised this works and there is no reverse to
    Warning: Missing argument 2 for Products. Why not something like Warning: Too many arguments for Products::__construct(),
    PHP Code:
    <?php
    error_reporting
    (E_ALL);
    class 
    Products
        
    {
            public function 
    __construct($type$length)
                {
                    echo 
    $type;    
                }
        }
    $object = new Products('DVD''4.5 hours''$24.00');
  2. #2
  3. Come play with me!
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    13,754
    Rep Power
    9397
    Because functions can accept any number of arguments beyond what their declarations require? See func_get_args, et al.
  4. #3
  5. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,926
    Rep Power
    1045
    Hi,

    I agree that this is somewhat strange. And it can lead to very ... weird situations.

    I remember a fellow programmer who would always pass a database connection as the second argument to some query function, assuming that the function would send the query to this connection. Well, it turns out the function only takes a single argument: the query. The feature of multiple connections never existed. Now our code is full of queries to some phantom database connection.

    Of course you can laugh about that and say that there are IDEs and that real men don't need p*ssy stuff like type safety or argument restrictions. But it would help sometimes.

    The PHP devs should have done what other languages do and explicitly mark functions as variadic, using some special parameter that takes all remaining arguments. But this is PHP.
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  6. #4
  7. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Location
    Iran
    Posts
    148
    Rep Power
    139
    Originally Posted by Jacques1

    The PHP devs should have done what other languages do and explicitly mark functions as variadic,using some special parameter that takes all remaining arguments. But this is PHP.
    That is true.

    I always use call by name method in my codes whenever I call a function. The code may become a little more wordy but personally I believe that it improves the code readability and maintenance because it's more obvious (comparing to the function signature) what has been provided exactly in terms of arguments.


    Regards,
    Dariyoosh
  8. #5
  9. Come play with me!
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    13,754
    Rep Power
    9397
    Originally Posted by Jacques1
    The PHP devs should have done what other languages do and explicitly mark functions as variadic, using some special parameter that takes all remaining arguments. But this is PHP.
    ...and this
    PHP Code:
    /**
     * Returns the minimum of all the given values
     *
     * @param mixed $values... One or more values
     * @return mixed
     */
    function minimum($value) { 
    is a docblock.
  10. #6
  11. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,926
    Rep Power
    1045
    Yes, in a perfect world, every bit of code is documented, everybody reads those documentations, and there are no bugs, ever.

    But have you ever wondered why some languages do extensive type checks, range checks, sometimes even checks for preconditions and postconditions? Why not just write it down with Javadoc etc.?

    It's because we do not live in a perfect world. If a language lets you make mistakes, then people will make those mistakes -- for all kinds of reasons.

    I mean, I admire your infinite trust in human perfection. Last time you told me that global escaping is unnecessary, because everybody knows the exact content of every single variable at any time. Great! We no longer have to worry about SQL injections, cross-site scripting and all that other stuff. The internet is secure now, we can all go home.

    However, when I look into this forum, I see new bugs and vulnerabilities and misunderstandings every day. So either those are all some kind of strange hallucinations. Or you seriously need to remove your rose-colored glasses.
    Last edited by Jacques1; March 17th, 2013 at 08:31 AM.
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  12. #7
  13. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Location
    Iran
    Posts
    148
    Rep Power
    139
    Originally Posted by Jacques1
    It's because we do not live in a perfect world. If a language lets you make mistakes, then people will make those mistakes -- for all kinds of reasons.
    If you're looking for such a well designed and robust language, then I suggest that you take a look (if you haven't already) at Ada programming language. As far as I know it's not a language for creating web applications, it is rather used to control trains without pilots, missiles, nuclear reactors, etc. So the environment in which applications are being executed is so critical that error is not permitted.

    But, if you investigate the methods used by the French Team who designed the language for the US Defence department, you will see that the design methods has nothing to do with many other languages. They used very complex mathematical/formal methods. They proved mathematically every component of the language core, particularly the very high and restrictive type system.

    It seems to me, that the language design is to some extent based and depends on the profile of the developers who are supposed to use that language. In other words, how much perfect a language design has to be, depends on how much perfect their users (that is, developers using that language) want to be.


    Regards,
    Dariyoosh

    Comments on this post

    • requinix agrees
  14. #8
  15. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,926
    Rep Power
    1045
    There's a lot of middle ground between a nuclear reactor and a PHP website.

    For example, many "web languages" like Ruby or Python use strong typing instead of weird automatic conversions. When you write down nonsense, the language will actually tell you that it's nonsense, it won't try to guess what you might have meant. When you suddenly try to use a variable that doesn't exist anywhere, the language won't accept that. You can't just dump GET or POST data into a query string without untainting it first. And so on.

    This prevents a lot of stupid errors without really limiting the programmer. When programming in Ruby, I've never felt the need for using undefined variables or accessing nonexistent indices in an array.

    You know what? I do in fact think that strict languages would solve many problems of the web. I've seen MySQL databases so broken that you had to guess the meaning of the data. I've seen PHP code so broken that you just stand there wondering how that ever got through the parser. But since PHP will accept pretty much anything, nobody noticed the errors until one day the statistics began to display weird numbers.

    Yes, in the 90s, it might have been a good idea that any moron could just bang his/her fist on they keyboard and would get a homepage from it. But since websites are getting more and more professional, it's no longer sufficient to display "something" in the browser. It has to be the correct thing, it has to be parseable by search engines etc.

    I think the times of "garbage in, garbage out" are over.
    Last edited by Jacques1; March 17th, 2013 at 09:59 AM.
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  16. #9
  17. No Profile Picture
    Lost in code
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 2004
    Posts
    8,301
    Rep Power
    7170
    When you write down nonsense, the language will actually tell you that it's nonsense, it won't try to guess what you might have meant.
    At least it will when the nonsense is syntactic. I have zero statistics to back me up, but I would guess that most bugs (across all programming languages) are caused by logic errors rather than syntax errors.

    I've seen MySQL databases so broken that you had to guess the meaning of the data.
    Relational database design is exactly the same in PHP as it is in any other language, strict or not.

    I think the times of "garbage in, garbage out" are over.
    I wouldn't bet on it. If as many people programmed in Ruby or Python as program in PHP we would see a lot of new and interesting ways of writing garbage code. As someone who I can't name once said, "Never underestimate the power of human stupidity."

    Also Ruby itself doesn't have a particularly good reputation as far as web development goes, although that is primarily because of Rails rather than the language itself.
    PHP FAQ

    Originally Posted by Spad
    Ah USB, the only rectangular connector where you have to make 3 attempts before you get it the right way around

IMN logo majestic logo threadwatch logo seochat tools logo