Newbie in PHP

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!

Moderator: General Moderators

Post Reply
ngolehung84
Forum Newbie
Posts: 6
Joined: Tue Jan 30, 2007 8:45 am

Newbie in PHP

Post by ngolehung84 »

feyd | Please use

Code: Select all

,

Code: Select all

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:

Code: Select all

class A
{
	var $loc; 
	function A($val)
	{
		$loc = $val;
	}	
}

$obj = new A(5);
echo $obj->loc;
When it execute, it doesnot display anything. Can you explain?
Thanks,
Hung.


feyd | Please use

Code: Select all

,

Code: Select all

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]
wildwobby
Forum Commoner
Posts: 66
Joined: Sat Jul 01, 2006 8:35 pm

Post by wildwobby »

to start make it $this->loc instead of just $loc here:

class A
{
var $loc;
function A($val)
{
$loc = $val;
}
}

$obj = new A(5);
james.aimonetti
Forum Newbie
Posts: 6
Joined: Tue Jan 30, 2007 10:44 pm

Post by james.aimonetti »

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.
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post by CoderGoblin »

For PHP5 the following will work

Code: Select all

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.
An alternative is

Code: Select all

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.

Hop that helps
Post Reply