Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

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!

Report this thread to moderator Post Follow-up to this message
Old Post
Bob
09-27-04 01:55 AM


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




Report this thread to moderator Post Follow-up to this message
Old Post
Janwillem Borleffs
09-27-04 01:55 AM


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




Report this thread to moderator Post Follow-up to this message
Old Post
Janwillem Borleffs
09-27-04 01:55 AM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

PHP Language archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 05:34 PM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.