SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    if($awake){code();} PHP John's Avatar
    Join Date
    Jul 2002
    Location
    Along the Wasatch Fault line.
    Posts
    1,771
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Cool Just starting into the world of OOP

    In PHP is it acceptable to overload a class contructor?

    Does PHP have "private, protected, and public" designators for class methods and variables, or is any variable/method accessable from outside the object?
    John

  2. #2
    "Of" != "Have" bronze trophy Jeff Lange's Avatar
    Join Date
    Jan 2003
    Location
    Calgary, Canada
    Posts
    2,063
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP4 considers every method and variable public (PHP5 does not).

    in PHP, there is no overloading, so I don't know if that is possible. (You can have optional arguments, or do a count of the args provided to the method, and the like, but there is no real overloading)
    Who walks the stairs without a care
    It shoots so high in the sky.
    Bounce up and down just like a clown.
    Everyone knows its Slinky.

  3. #3
    if($awake){code();} PHP John's Avatar
    Join Date
    Jul 2002
    Location
    Along the Wasatch Fault line.
    Posts
    1,771
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    So, basically, I could set up a class constructor to acccept an array of arguments and then test for values in the array, and any needed value not passed in with the array I would then have a default value ready to assign.

    Have I got that right?
    John

  4. #4
    "Of" != "Have" bronze trophy Jeff Lange's Avatar
    Join Date
    Jan 2003
    Location
    Calgary, Canada
    Posts
    2,063
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That's one way to do it for sure.

    another is like this:
    PHP Code:
    class testClass
    {
        function 
    testClass()
        {
            
    $numargs func_num_args();
            echo 
    "Number of arguments: $numargs<br>\n";
            if (
    $numargs >= 2)
            {
                echo 
    "Second argument is: " func_get_arg (1) . "<br>\n";
            }
        }

    Who walks the stairs without a care
    It shoots so high in the sky.
    Bounce up and down just like a clown.
    Everyone knows its Slinky.

  5. #5
    "Of" != "Have" bronze trophy Jeff Lange's Avatar
    Join Date
    Jan 2003
    Location
    Calgary, Canada
    Posts
    2,063
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    func_get_args() 
    just looked that up too, it gives you an array of every argument given to a function.
    Who walks the stairs without a care
    It shoots so high in the sky.
    Bounce up and down just like a clown.
    Everyone knows its Slinky.

  6. #6
    Non-Member coo_t2's Avatar
    Join Date
    Feb 2003
    Location
    Dog Street
    Posts
    1,819
    Mentioned
    1 Post(s)
    Tagged
    1 Thread(s)
    I think there's at 3 reasons why having method overloading isn't really needed.

    1. PHP is loosely typed.
    2. Functions/methods can have default values.
    3. Functions/methods can have variable length
    parameter lists via the func_get_args() function.

    In PHP 4 when people needed to simulate method overloading you seen
    them use func_get_args() a lot to decide which version of the method to call. I suppose you'll still see this done in PHP 5 from time to time.

    --ed

  7. #7
    if($awake){code();} PHP John's Avatar
    Join Date
    Jul 2002
    Location
    Along the Wasatch Fault line.
    Posts
    1,771
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hmm... Great! Thanks!
    John


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
  •