#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2000
    Posts
    0
    Rep Power
    0
    I just started with php today, so I'm not completely familiar with it's objects yet. Well to the problem:

    class foo {

    var bar;

    function foo () {
    $this->bar = "foobar";
    }

    function foo( $string ) {
    $this->bar = $string;
    }
    }

    So how can I have many functions in a class with the same name, I'd like the parser to which one of them to call based on the parametres.

    One approach would be to use default value like:

    class foo {

    var bar;

    function foo ( $string = "foobar" ) {
    $this->bar = $string;
    }
    }

    But that's not completely what I want. So is this C++ class thingy available in php atleast to some degree? I know it's hard to implement, as the type on php variables can change on the fly, so it would be quite difficult to avoid ambigious functions, but anyways is there some way to do it?
  2. #2
  3. Banned (not really)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 1999
    Location
    Brussels, Belgium
    Posts
    14,642
    Rep Power
    4492
    I don't really understand what your asking. You can't have two functions with the same name, no matter what.

    When you define a class, if you create a function within the class with the same name as the class, it's called a constructor. When you create a new object, this function is called.

    class foo
    {
    var $bar;

    function foo()
    {
    $this->bar = "some_val";
    }

    function change($string)
    {
    $this->bar = $string;
    }
    }

    $obj = new foo();

    echo $obj->bar;
    //should echo some_val

    $obj->change("another_val");

    echo $obj->bar;
    //should echo another_val

    My OOP is a little rusty, but do you get the idea??

    ---John Holmes...

    ------------------
    *************************************************************
    * The manual can probably answer 90% of your questions...
    *
    * PHP Manual. www.php.net/manual
    * MySQL Manual: www.mysql.com/documentation/mysql/bychapter
    *************************************************************

    [This message has been edited by SepodatiCreations (edited October 09, 2000).]
  4. #3
  5. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2000
    Posts
    0
    Rep Power
    0
    Well seems like you don't have too much C++ experience, otherwise you'd know what I meant

    ok so this is basically what I'm doing, I already got support for what I wanted by doing a huge hack...

    class SQL {

    ...

    function get_row ( $result = string1 ) {
    if (strlen($result) > 20) {
    eval($result)
    } else {
    mysql_get_row ( $result );
    and continue as we should...
    }

    Then string1 is:
    mysql_get_row( $this->result );
    ...

    this way I can do like:
    $foo = new SQL;
    ...
    $result = blabla;
    $row = get_row( $result );
    or I could call just:
    $row = get_row();
    this way I can either ask for a row in the last query or a specific one, something that is really cool about C++ classes (though x86 asm is a lot cooler than C++), but anyway if someone could come up with a simple way to implement this in "pure" PHP, great!

    [This message has been edited by desti (edited October 09, 2000).]
  6. #4
  7. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2000
    Posts
    0
    Rep Power
    0
    Blah, I'm stupid, the easiest way to implement that previous case would have been to do something like:

    class SQL
    ...
    function foo ( $result = NULL )
    if ( $result == NULL ) {
    $foo = mysql_fetch_row ( $this->result);
    } else {
    $foo = mysql_fetch_row ( $result );
    }
    ...
    }

    But now I know it's possible to do what I tried previously

    Well it seems like php doesn't have support for many contructors etc in one class, so I need to find a solution to those cases where i need to have 3 or more ways to call a function... but anyway thanks for your help...

    -desti
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2000
    Posts
    114
    Rep Power
    15
    PHP is not a typed language like C or C++ so function overloading doesn't really make sense. However, it does have a set of variable operators that can tell you just what type of variable you have. Together with a switch statement, you can acheive similar functionalilty:

    <BLOCKQUOTE><font size="1" face="Verdana,Arial,Helvetica">code:</font><HR><pre>
    function multiTasker($arg)
    {
    switch ($arg)
    {
    case is_array($arg):
    // code for an array
    break;
    case is_object($arg):
    // code for objects
    break;
    }
    }
    [/code]
    There's an 'is_' for just about every data type. Also, OOP in PHP comes at a slight performance cost, so don't over do it.
  10. #6
  11. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2000
    Posts
    0
    Rep Power
    0
    Well most of what I wanted can be done by giving default values to the parametres. I know there's no way for the parser to find any differences between functions that have the same name and the same amount of arguments.

    Yes I looked at the PHP parser's code and noticed that it has to go through a hell of a lot more code when doing OOP. Anyways I'm more concerned about the clarity of the code and easy management, so I'm going to do some tradeoffs between speed and clarity. What I've tried to optimize the most is the amount of queries done to the databases, so anything that would require less processing for the database, but more to do for the webserver, will get in (as long as the webserver can cope with the load).

    Though the only thing I'm going to have in different classes is DB access and a few other things like building forms, as I need to support MySQL, PostgreSQL, DB2 and Oracle.

    ps. Anyone know any page with PHP benchmarks?
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2000
    Posts
    114
    Rep Power
    15
    You could go to www.zend.com and look around in the articles and case studies. There's also a similar section on www.php.net , look for php in the news section. I've never seen a completely objective, standalone bench mark test of PHP (if you find one come back and post a link). Most of the time it just gets compared to ASP. Yes, PHP is faster.

    [This message has been edited by billyo (edited October 09, 2000).]

Similar Threads

  1. Replies: 2
    Last Post: August 18th, 2004, 10:45 PM
  2. How to see the Memory used by a PHP script?
    By Pardall in forum PHP Development
    Replies: 4
    Last Post: February 13th, 2004, 03:54 PM
  3. PHP with UnixODBC
    By coder4hire in forum PHP Development
    Replies: 2
    Last Post: January 20th, 2004, 01:14 PM
  4. Passing string from PHP to Javascript
    By ka8oad in forum JavaScript Development
    Replies: 4
    Last Post: January 17th, 2004, 03:12 PM
  5. UnixODBC + PHP + Apache + Empress
    By coder4hire in forum Database Management
    Replies: 0
    Last Post: January 15th, 2004, 01:41 PM

IMN logo majestic logo threadwatch logo seochat tools logo