/    Sign up×
Community /Pin to ProfileBookmark

help with return function

hi all,
i have one question around return function
for ex.

[code=php]<?php
class Employee
{
private $name;
// Getter
public function getName() {
return $this->name;
}
// Setter
public function setName($name){
$this->name = $name;
}
}
$azer=new Employee();
$azer->setName(‘sahib’);
echo $azer->getName();
?>[/code]

why here we is used two functions. one serve as getting values and the other one serve as setting values. Why we cannot use for this both getting values and setting together inside one function. such as below.

[code=php]<?php
class Employee
{
private $name;
// Getter

// Setter
public function setName($name){
$this->name = $name;
return $this->name;
}
}
$azer=new Employee();
$azer->setName(‘sahib’);
echo $azer->setName();
?>[/code]

can anyone help me if possible
thanks in advance

to post a comment
PHP

6 Comments(s)

Copy linkTweet thisAlerts:
@tirnaJun 20.2010 — you could do it that way but it is not the way it is normally done.

A class (object) method (whether it is PHP, Java etc doesn't matter ) is meant to perform a single function only.

For example, in your code you might need to "set" the value of a class property at some stage without needing it to then be returned at that point and elsewhere in your code you might just need to "get" the value of the property [B]without having to set it to its current value first, [/B]like you do in your suggestion.
Copy linkTweet thisAlerts:
@azegurbauthorJun 20.2010 — thank you very much

but i again dont understand why we cannot set values and get values inside one function

for ex. we build first the fucntion that sets values and second fucntion is for retunrning values that is set at the first function. Cannot we set and return values inside one function.

thanks for attention
Copy linkTweet thisAlerts:
@tirnaJun 20.2010 — no problem ?

[U]I did say you can do both set and get in one function if you like.[/U] It's just not normally done that way for the reasons I expressed earlier.


With your code:

[code=php]
$azer=new Employee();
$azer->setName('sahib');
echo $azer->setName();
[/code]


imo you have an unnecessary line of code because instead of the 3 lines you have above you could get the same output with:

[code=php]
$azer=new Employee();
echo 'sahib';
[/code]
Copy linkTweet thisAlerts:
@MindzaiJun 20.2010 — The code you posted is not valid. You cannot define an argument and then not provide in calls from client code. You could make the argument optional and have logic to handle getting or setting conditionally, but why would you want to do that? Surely it is much simpler and clearer to have get and set functions?
Copy linkTweet thisAlerts:
@azegurbauthorJun 20.2010 — thank you very much

can you show me (if possible) the need of creating getter function
Copy linkTweet thisAlerts:
@azegurbauthorJun 20.2010 — solved
×

Success!

Help @azegurb spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.4,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...