PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
I have this code block:
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
If you are using PHP5, I think it is more proper to declare member variables using public / private / protected. http://www.php.net/manual/en/language.oop5.php outlines the functionality and code used in creating classes with PHP5. For PHP4, see http://www.php.net/manual/en/language.oop.php to educate yourself on the implementation of OOP there, which differs in some respects from PHP5's implementation. Personally, I'd be sure PHP5 was being used if you have control over that decision.
class A
{
var $loc;
public function __construct($value)
{
$this->loc=$value;
}
public function getLoc()
{
return $this->loc;
}
public function setAndGetLoc($value)
{
$this->loc=$value;
return $this->getLoc();
}
}
$obj = new A(5);
echo $obj->getLoc(); // returns the value of the constructor (5)
echo $obj->getAndSetLoc(8); // Changes the Loc value and returns it.
class A
{
var $loc;
public function __construct($value)
{
$this->loc=$value;
}
public static function getLoc($value)
{
$loc=$value;
return $loc;
}
}
echo A::getLoc($value);
One of the primary things you will notice is that the class methods return something, as a normal function can. This is the value you are setting, in your case an echo to. This is same in PHP4 and PHP5.