runeveryday Posted August 8, 2009 Share Posted August 8, 2009 i have read some tutorials about the class of php.but i can't understand it clearly.a friend of mine give me an instance. class username(){ var $username; function printName(){ echo $this->username; } function setName($name){ $this->username = $name; } } $object = new username; $object->setName('runeveryday'); echo "My name is : " . $object->getName(); i konw state a class is class classname{ } but in my friend's example the form is class classname(){ } is it right? who can give me the details about the class. Any hint would be GREATLY appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/169322-the-class-of-php/ Share on other sites More sharing options...
trq Posted August 8, 2009 Share Posted August 8, 2009 Your friends syntax is incorrect. Quote Link to comment https://forums.phpfreaks.com/topic/169322-the-class-of-php/#findComment-893483 Share on other sites More sharing options...
runeveryday Posted August 8, 2009 Author Share Posted August 8, 2009 Your friends syntax is incorrect. thanks! would you mind telling me the mean of the above example. Quote Link to comment https://forums.phpfreaks.com/topic/169322-the-class-of-php/#findComment-893486 Share on other sites More sharing options...
trq Posted August 8, 2009 Share Posted August 8, 2009 Its a pretty useless example. It (the object) simply stores a name ( setName() ) and can also return that name ( getName() ). Its pretty self explanatory. Quote Link to comment https://forums.phpfreaks.com/topic/169322-the-class-of-php/#findComment-893487 Share on other sites More sharing options...
trq Posted August 8, 2009 Share Posted August 8, 2009 Actually, looking at it again, the code is a little messed up. Should be.... class username { private $username; function getName(){ return $this->username; } function setName($name){ $this->username = $name; } } $object = new username; $object->setName('runeveryday'); echo "My name is : " . $object->getName(); Quote Link to comment https://forums.phpfreaks.com/topic/169322-the-class-of-php/#findComment-893489 Share on other sites More sharing options...
runeveryday Posted August 8, 2009 Author Share Posted August 8, 2009 could you give me a better example to explain how the php class works? the more datails,the more better! thank you very much! Quote Link to comment https://forums.phpfreaks.com/topic/169322-the-class-of-php/#findComment-893498 Share on other sites More sharing options...
trq Posted August 8, 2009 Share Posted August 8, 2009 Its all pretty well covered in the manual and many other tutorials on the net. If you can't yet understand that basic example, I'm probably not going to be able to explain it in a quick post, I'll try though. Firstly, you have a class. class username { } In order for this class to actually store a variable within itself, you need to declare that variable. class username { private $name; } We use the keyword private here because we only want that variable available within the class itself. Now, because that variable is only available within that class, we need to create a function (method) to set that variable to something.... class username { private $name; function setName($var) { $this->name = $var; } } The $this keyword is used to address a variable (or a method) within the object (a class becomes an object once instantiated, more on this later) itself. We then need a method to access our $name variable from outside the class. class username { private $name; function setName($var) { $this->name = $var; } function getName() { return $this->name; } } Thats it, the entire class. Now to the client code (the code that makes use of the class). // firstly we instantiate the class and assign the returned object to a variable. $obj = new username; // we can now set the username variable stored within our object. $obj->setName('foo'); // and now we can retrieve it. echo $obj->getName(); Quote Link to comment https://forums.phpfreaks.com/topic/169322-the-class-of-php/#findComment-893501 Share on other sites More sharing options...
runeveryday Posted August 8, 2009 Author Share Posted August 8, 2009 many thanks! you let me know a lot! thank you! Quote Link to comment https://forums.phpfreaks.com/topic/169322-the-class-of-php/#findComment-893510 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.