Arty Ziff Posted February 11, 2012 Share Posted February 11, 2012 I'm looking for a php class tutorial that talks about some very basic things that don't seem to get talked about in begaining php class tutorials. Such as declaring variables within a class and when you should do that. And within a class what is the difference between a pubic and private function? The function thing I can guess at: private functions are only available for use within the class, but public functions can be called from outside the class, yes? Just a guess. But also I am confused about when I should declare variables within a class. For example I recently saw: class something { private $this, $that, $theOtherThing; public function someFunction { ... } } Isn't there some PHP Classes For Idiots that covers this stuff? Quote Link to comment Share on other sites More sharing options...
Proletarian Posted February 11, 2012 Share Posted February 11, 2012 http://www.php.net/manual/en/language.oop5.basic.php Quote Link to comment Share on other sites More sharing options...
trq Posted February 11, 2012 Share Posted February 11, 2012 As has been pointed out, your questions will be answered in the manual. Where to put your properties? That is more a style thing as php doesn't care. Everyone always declares them before any methods though. Iv'e not seen anyone do any differently. Quote Link to comment Share on other sites More sharing options...
ignace Posted February 11, 2012 Share Posted February 11, 2012 Everyone always declares them before any methods though. Iv'e not seen anyone do any differently. I've seen it be done on at least one occasion like: class ExampleFoo { private $bar; public function doStuffWithBar() {} private $bat; public function doStuffWithBat() {} } Quote Link to comment Share on other sites More sharing options...
Arty Ziff Posted February 11, 2012 Author Share Posted February 11, 2012 The Proletariat hit it on the head: RTFM... It does answer the questions. Although I kind of like tutorials with copious examples... But I guess TM does cover the *basics* of my question... Quote Link to comment Share on other sites More sharing options...
Proletarian Posted February 11, 2012 Share Posted February 11, 2012 The Proletariat hit it on the head: RTFM... It does answer the questions. Although I kind of like tutorials with copious examples... But I guess TM does cover the *basics* of my question... Not to sound rude, but it's not a complicated concept. I think of it like this: A class is an abstract object that has dimensions (properties/variables) and can do things (methods/functions). When a class is instantiated, the object will have measurable properties and has the capacity to act. - Class declaration - Public, private, protected properties - Public, private, protected methods Basically, all properties of certain scope in one area and all methods of certain scope in the second area. Public scope is basically anything I want to access straight from the object. Typically, I don't make properties public, I make them private/protected and access/modify them with public methods. To put it in existential terms, if I were an instance of a class, only I would be aware of the values of my (private) properties, while if I wanted to use them or if you wanted to know about me, we would make use of our (public) methods to discover those values or fiddle with them in some way. In code terms, it's like this: <?php class person { //======================================== /*======================================== THINGS THAT DESCRIBE AND QUANTIFY ALL INSTANCES OF THIS CLASS! ========================================*/ private $myName; private $myAge; private $myHeight; private $myWeight; //======================================== /*======================================== THINGS THAT ALL INSTANCES OF THIS CLASS CAN DO! ========================================*/ public function __construct($n, $a, $h, $w) { $this->myName = $n; $this->myAge = $a; $this->myHeight = $h; $this->myWeight = $w; } //======================================== public function change_name($s) { $this->myName = $s; } public function tell_name(&$s) { $s = $this->myName; } //======================================== public function show_age(&$s) { $s = $this->myAge; } public function live_another_day() { $this->myAge++; } //======================================== public function weigh(&$s) { $s = $this->myWeight; } public function measure(&$s) { $s = $this->myHeight; } //======================================== public function lose_weight() { $this->myWeight--; } public function gain_weight() { $this->myWeight++; } //======================================== } /*======================================== AN EXAMPLE OF HOW TWO INSTANCES OF THE SAME CLASS USE THE SAME METHOD AND PROPERTY OUTLINES BUT ACHIEVE INDIVIDUALLY UNIQUE RESULTS. ========================================*/ public function blah() { $me = new person("Alice", 25, 100, 100); $you = new person("Bob", 50, 200, 200); $me->get_name($name); // SHOWING MY NAME echo $name; $you->get_name($name); // SHOWING YOUR NAME echo $name; $me->change_name($name); // CHANGING MY NAME TO YOUR NAME $me->get_name($name2); // SHOWING MY NEW NAME echo $name2; } ?> I hope this example is tutorial enough. If not, well, I hope someone else can expand upon my "tutorial" further. Quote Link to comment Share on other sites More sharing options...
KevinM1 Posted February 11, 2012 Share Posted February 11, 2012 A class is an abstract object Ooh, that's a bad way to describe classes, especially given the language construct of abstract classes. More to the point, classes are NOT objects. The two terms have very distinct definitions in OOP. Classes are definitions. They describe what individual objects are - what they can contain, how they act, etc. No more, no less. It's a bit awkward to describe access control in terms of scope as the object itself will always have complete and total access to everything in its class. Access control defines how code external to an object of that particular class can interact with it. Quote Link to comment 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.