1. Change it directly..
Code: Select all
$object->var = 1;2. Use a method to change it..
Code: Select all
$object->changeVar(1);Moderator: General Moderators
Code: Select all
$object->var = 1;Code: Select all
$object->changeVar(1);When you are working in an Object Oriented environment, using Objects, then it is not only sensible but it is standard. One of the main ideas behind OO is the abstraction of data. When creating an object the developer is trying to control how each of the private members are being used and accessed. Generally, each private variable in an object is has two associated public members that are used to get and set the value of that private variable. The programmer then can control how each variable is set and what values are acceptable. If a programmer wants to have a private variable be read only then the programmer can only specify a get method for that private variable. This makes it so that only internal methods have access to that variable.onion2k wrote:Is there any sensible reason not to allow people to access to variables that are public directly? Using a method would allow me to validate things, but for the time being thats really not necessary.
Code: Select all
<?php
class MyClass
{
protected $myVar;
function getMyVar ()
{
return $myVar;
}
function setMyVar ($val)
{
$this->myVar = $val;
}
}
?>Code: Select all
<?php
class test
{
public $test;
function setObjVar ($var, $val)
{
if (array_key_exists($var, get_object_vars($this))) {
$this->{$var} = $val;
} else {
trigger_error('Invalid class (' . __CLASS__ . ') property specified: ' . $var . '!', E_USER_ERROR);
}
}
function getObjVar ($var)
{
if (array_key_exists($var, get_object_vars($this))) {
return $this->{$var};
} else {
trigger_error('Invalid class (' . __CLASS__ . ') property specified: ' . $var . '!', E_USER_ERROR);
}
}
}
$test = new test;
$test->setObjVar('test', 'yes');
print $test->getObjVar('test') . "\n";
?>If your following proper OOP protocol and using data accessor's/mutator's why then, would you declare your member data as protected?Jenk wrote:Is the way I do itCode: Select all
<?php class MyClass { [b]protected $myVar;[/b] function getMyVar () { return $myVar; } function setMyVar ($val) { $this->myVar = $val; } } ?>
If a class has lots of properties.. I sometimes get lazy.. but this is not a good practice, imo.
Code: Select all
<?php class test { public $test; function setObjVar ($var, $val) { if (array_key_exists($var, get_object_vars($this))) { $this->{$var} = $val; } else { trigger_error('Invalid class (' . __CLASS__ . ') property specified: ' . $var . '!', E_USER_ERROR); } } function getObjVar ($var) { if (array_key_exists($var, get_object_vars($this))) { return $this->{$var}; } else { trigger_error('Invalid class (' . __CLASS__ . ') property specified: ' . $var . '!', E_USER_ERROR); } } } $test = new test; $test->setObjVar('test', 'yes'); print $test->getObjVar('test') . "\n"; ?>
Depends who you ask...onion2k wrote:Quick question. I have an object with a variable contained inside it. In order to change that variable would you prefer to:
1. Change it directly..orCode: Select all
$object->var = 1;
2. Use a method to change it..Is there any sensible reason not to allow people to access to variables that are public directly? Using a method would allow me to validate things, but for the time being thats really not necessary.Code: Select all
$object->changeVar(1);
I'm OOP zealot (kind of), and I'd say 1 might not be such a bad idea in some cases.OOP Zealots will always say number two...
Code: Select all
$obj->setVar($obj->getVar2());
//vs
$obj->var = $obj->var2;The "sensible reason" historically given to use a method rather than accessing properties directly was that it abstracts theaccess. By that I mean that if you need to change the behavior or do some processing of a property -- that is easily done using a method -- but can be a problem in PHP if you access them directly. What if, for example, you wanted to do bounds checking on a property when it was being set?onion2k wrote:Is there any sensible reason not to allow people to access to variables that are public directly? Using a method would allow me to validate things, but for the time being thats really not necessary.
Kind of eh?Gambler wrote:I'm OOP zealot (kind of), and I'd say 1 might not be such a bad idea in some cases.OOP Zealots will always say number two...
First, it's much cleaner syntactically in case of complicated assignments:Second, it might give you considerable speed avantage if you access variable thousands of times. Methods have overhead.Code: Select all
$obj->setVar($obj->getVar2()); //vs $obj->var = $obj->var2;
Also, it might worth checking __get and __set magic methods.
Abstract classes make the use of private not always appropriate, and using protected also allows the method's to be approriately overloaded/redefined if the child class requires they are to be done so.Hockey wrote: If your following proper OOP protocol and using data accessor's/mutator's why then, would you declare your member data as protected?
private would be a better choice...child classes would then be forced to use the appropriate member functions and you avoid ever directly accessing member data in derived objects.
Cheers
Code: Select all
<?php
class A
{
private $var;
function setVar ($val)
{
$this->var = $val;
}
function getVar ()
{
return $this->var;
}
}
class B extends A
{
function setVar ($val)
{
$this->var = $val;
echo "done!\n";
}
}
$a = new A;
$b = new B;
$a->setVar('a');
$b->setVar('b');
print $a->getVar()."\n";
print $b->getVar()."\n";
?>Code: Select all
class BankAccount{
private $balance = 0.0;
public BankAccount(){
}
public getBalance(){
return $balance
}
private setBalance($balance){
$this->balance = $balance;
}
public modifyBalance($balance, $user, $date, $someNecessaryFields){
if (checkAllNecessaryConditionsSatisfied){
$this->setBalance($balance);
}else{
return false;
}
}
}
$accountInstance = new BankAccount();
$accountInstance->balance = 1000; //this should not be accessible
$accountInstance->modifyBalance(1000, $accepted_creditor, $date, $allOtherNecessaryFields);
$accountInstance->getBalance(); //this is how you should see the member variable