Professional Web Applications Themes

Problem with using func_num_args to emulate get/set? - PHP Development

I'm using func_num_args to create a get/set type of function. Will this have unpredictable results, or or lead to some sort of problem I'm just not aware of? (I'm a bit of a newbie w/PHP.) Here's an example: class MyClass { var $name; function var_name($value) { if (func_num_args() == 0) // if there's no arg, get return $this->name; $this->name = $value; // else set } } $my_obj = new MyClass; $my_obj->var_name("Rob"); // set $my_obj->name to 'Rob' $name = $my_obj->var_name(); // now set $name to 'Rob' Using PHP 4.3.4, thanks!...

  1. #1

    Default Problem with using func_num_args to emulate get/set?

    I'm using func_num_args to create a get/set type of function. Will this
    have unpredictable results, or or lead to some sort of problem I'm just not
    aware of? (I'm a bit of a newbie w/PHP.)

    Here's an example:

    class MyClass
    {
    var $name;
    function var_name($value)
    {
    if (func_num_args() == 0) // if there's no arg, get
    return $this->name;
    $this->name = $value; // else set
    }
    }

    $my_obj = new MyClass;
    $my_obj->var_name("Rob"); // set $my_obj->name to 'Rob'
    $name = $my_obj->var_name(); // now set $name to 'Rob'


    Using PHP 4.3.4, thanks!
    Bob Guest

  2. #2

    Default Re: Problem with using func_num_args to emulate get/set?

    Bob wrote:
    > I'm using func_num_args to create a get/set type of function. Will
    > this have unpredictable results, or or lead to some sort of problem
    > I'm just not aware of? (I'm a bit of a newbie w/PHP.)
    >
    Not really, but it's a better practice to use defaults for the arguments:

    function var_name($value = "") {
    $this->name = $value;
    }

    If you want to set $name only when the argument isn't empty, you can do
    something like this:

    function var_name($value = "") {
    if ($value) {
    $this->name = $value;
    }
    }

    And now for naming; the function and variable names should represent there
    purpose. Since this is a setter which uses the argument to set a variable
    with the name $name, consider the following:

    function setName($name = "") {
    if ($name) {
    $this->name = $name;
    }
    }
    > $name = $my_obj->var_name(); // now set $name to 'Rob'
    >
    In proper class designs, setter and getter methods are seperated, so
    consider a method called getName() which returns the value of the $name
    variable instead:

    $name = $my_obj->getName(); // now set $name to 'Rob'


    HTH;
    JW



    Janwillem Borleffs Guest

  3. #3

    Default Re: Problem with using func_num_args to emulate get/set?

    Janwillem Borleffs wrote:
    > $name = $my_obj->getName(); // now set $name to 'Rob'
    >
    Of course, the comment should be:

    // now get $name


    JW



    Janwillem Borleffs Guest

Similar Threads

  1. How to emulate F5 (Refresh) for the currently editeddoent?
    By harpo_ in forum Macromedia Exchange Dreamweaver Extensions
    Replies: 3
    Last Post: March 21st, 10:27 AM
  2. Best way to emulate number of requests cap
    By ck128 in forum ASP.NET Web Services
    Replies: 1
    Last Post: May 18th, 05:41 AM
  3. Emulate a explorer window
    By Angel in forum ASP.NET Building Controls
    Replies: 2
    Last Post: February 4th, 12:36 PM
  4. Emulate perls local
    By Karsten Meier in forum Ruby
    Replies: 4
    Last Post: September 9th, 09:07 PM
  5. How can I emulate my browsers HTTP_USER_AGENT?
    By Randell D. in forum PHP Development
    Replies: 1
    Last Post: July 10th, 09:11 AM

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
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139