which one to use and why ??
Posted: Tue Jul 03, 2007 5:56 am
Hello forums!!
I am curious about some question which i am going to mention here.
we know the advantage of using getters and setters in OOP.
Let us consider the case:
My Question is very simple..
once the property is set using methods, for internal use in the class file
which one to use and why ?
1> $value = $this->prop1 + $this->prop2;
2> $value = $this->getProp1() + $this->getProp2();
Thanks in advance to all of you.
I am curious about some question which i am going to mention here.
we know the advantage of using getters and setters in OOP.
Let us consider the case:
Code: Select all
<?php
class className{
var prop1;
var prop2;
var prop3;
function className(){
// Empty constructor
}
function setProp1($prop1){
$this->prop1 = $prop1;
}
function getProp1(){
return $this->prop1;
}
function setProp2($prop2){
$this->prop2 = $prop2;
}
function getProp2(){
return $this->prop2;
}
function addProp12(){
$value = $this->prop1 + $this->prop2;
// Alt $value = $this->getProp1() + $this->getProp2();
return $value;
}
}
// Using Class
$classObj = new className();
$classObj->setProp1("x");
$classObj->setProp2("y");
echo $classObj->addprop12();
?>once the property is set using methods, for internal use in the class file
which one to use and why ?
1> $value = $this->prop1 + $this->prop2;
2> $value = $this->getProp1() + $this->getProp2();
Thanks in advance to all of you.