wright67uk Posted April 8, 2012 Share Posted April 8, 2012 I'm trying to get my head around oop and wondered if anyone would look at my comments so far . I want to learn a bit about constructors next but thought I should make sure I understand the below code properly first. <?php class Dictionary { //new class public $translations = array(); public $type =" "; //new variables called 'PROPERTIES' function summarize() { //function within a class is called a 'METHOD' $ret = "Dictionary type: {$this->type}\n"; // {$this->type} means: use $type which is found within THIS class $ret .= "Terms: ".count( $this->translations )."\n"; // {$this->type} means: count the length of the $translation array which is found within THIS class return $ret; } } ?> <?php include('class_dictionary.php'); //include file that holds the class $english = new Dictionary; // creating new object using the class called Dictionary $english->type = "EN"; // adding a value to the 'type' property located within the dictionary class $english->translations['TREE'] = "branch"; //adding TREE key and value branch to the translations array $english->translations['TREEtwo'] = "trunk"; //adding TREEtwo key and value trunk to the translations array $french = new Dictionary; //creating a new instance of the Dictionary object $french->type = "FR"; $french->translations['TREE'] = "arbre"; print_r( $english ); echo "<br/>"; print $english->summarize(); //prints the summarize method ?> Quote Link to comment https://forums.phpfreaks.com/topic/260579-please-could-you-comment-on-my-comments-oop-beginner/ Share on other sites More sharing options...
freelance84 Posted April 8, 2012 Share Posted April 8, 2012 The var $french will over write the $english there... if you use the clone operator you will avoid this. I think this is what you meant?? <?php include('class_dictionary.php'); //include file that holds the class $english = new Dictionary; $english->type = "EN"; $english->translations['TREE'] = "branch"; //start the clone here $french = clone $english; $french->type = "FR"; $french->translations['TREE'] = "arbre"; print $english->summarize(); //this will print the value from english print $french->summarize(); //this will print the value from french ?> If you don't clone as above the output would be both the french inputs... and too much french is never a good thing Quote Link to comment https://forums.phpfreaks.com/topic/260579-please-could-you-comment-on-my-comments-oop-beginner/#findComment-1335484 Share on other sites More sharing options...
scootstah Posted April 9, 2012 Share Posted April 9, 2012 The var $french will over write the $english there... No, it won't. He created a new instance of the class, so he has two different objects. Quote Link to comment https://forums.phpfreaks.com/topic/260579-please-could-you-comment-on-my-comments-oop-beginner/#findComment-1335499 Share on other sites More sharing options...
freelance84 Posted April 9, 2012 Share Posted April 9, 2012 Ah crap, sorry i misread that! hmm.. well there's another way to achieve the same thing anyway Quote Link to comment https://forums.phpfreaks.com/topic/260579-please-could-you-comment-on-my-comments-oop-beginner/#findComment-1335502 Share on other sites More sharing options...
KevinM1 Posted April 9, 2012 Share Posted April 9, 2012 @wright67uk, you're on the right track. From a design standpoint, however, you'll be better off labeling your properties as private and accessing them through public methods where necessary. One of the main points of OOP is encapsulation. Data should be encapsulated within an object, accessed from the outside through clear and explicit means. Leaving properties public destroys encapsulation, allowing them to be changed at a whim. Quote Link to comment https://forums.phpfreaks.com/topic/260579-please-could-you-comment-on-my-comments-oop-beginner/#findComment-1335520 Share on other sites More sharing options...
wright67uk Posted April 9, 2012 Author Share Posted April 9, 2012 thank you for the replies. KevinM1 Would you be kind enough to provide an example of how I can access my properties once they have been changed to private? Cheers, Ed Quote Link to comment https://forums.phpfreaks.com/topic/260579-please-could-you-comment-on-my-comments-oop-beginner/#findComment-1335561 Share on other sites More sharing options...
trq Posted April 9, 2012 Share Posted April 9, 2012 class Foo private $bar; public setBar($bar) { $this->bar = $bar; } public getBar() { return $this->bar; } } Quote Link to comment https://forums.phpfreaks.com/topic/260579-please-could-you-comment-on-my-comments-oop-beginner/#findComment-1335563 Share on other sites More sharing options...
wright67uk Posted April 9, 2012 Author Share Posted April 9, 2012 To end up like this? <?php class Dictionary { //new class public $translations = array(); private $type =" "; //new variables called 'PROPERTIES' public setBar($type) { $this->type = $type; } public getBar() { return $this->type; } function summarize() { //function within a class is called a 'METHOD' $ret = "Dictionary type: {$this->type}\n"; // {$this->type} means: use $type which is found within THIS class $ret .= "Terms: ".count( $this->translations )."\n"; // {$this->type} means: count the length of the $translation array which is found within THIS class return $ret; } } ?> sorry if im not getting this! Quote Link to comment https://forums.phpfreaks.com/topic/260579-please-could-you-comment-on-my-comments-oop-beginner/#findComment-1335571 Share on other sites More sharing options...
trq Posted April 9, 2012 Share Posted April 9, 2012 Kinda on the right track. You would call your methods setType() and getType() though. Doesn't make much sense using Bar. Quote Link to comment https://forums.phpfreaks.com/topic/260579-please-could-you-comment-on-my-comments-oop-beginner/#findComment-1335575 Share on other sites More sharing options...
Muddy_Funster Posted April 9, 2012 Share Posted April 9, 2012 As your post was to comment on your comments, all I'd suggest is maybe including a doc block for each class with the declarations in it. I've just started looking into the OOP side of things myself and one of the things I have come accross is that, in some API implimentations, these doc blocks are really rather important for some DI patterns. One of the newer (newest?) ZEND releases can be coded to build refferences automaticly using the doc blocks as a kind of catalogue (that's my interpretation anyway, but like I said I'm new to this OOP thing too) for building the DI relations. I suck at OOP, having just started playing with it and coming from more than a few years in the procedural world, so I can't really say more than that. I'm sure one of the better programmers will corect what I've got wrong and clear up the bit's I didn't. Quote Link to comment https://forums.phpfreaks.com/topic/260579-please-could-you-comment-on-my-comments-oop-beginner/#findComment-1335577 Share on other sites More sharing options...
wright67uk Posted April 9, 2012 Author Share Posted April 9, 2012 ok i kinda get this but line 10 - public setBar($type) gives me a Parse error: syntax error, unexpected T_STRING, expecting T_VARIABLE Quote Link to comment https://forums.phpfreaks.com/topic/260579-please-could-you-comment-on-my-comments-oop-beginner/#findComment-1335590 Share on other sites More sharing options...
trq Posted April 9, 2012 Share Posted April 9, 2012 As I said, using setBar doesn't really make any sense does it? Fix it, and post your current code. Quote Link to comment https://forums.phpfreaks.com/topic/260579-please-could-you-comment-on-my-comments-oop-beginner/#findComment-1335592 Share on other sites More sharing options...
wright67uk Posted April 9, 2012 Author Share Posted April 9, 2012 dictionary.php <?php include('class_dictionary.php'); //include file that holds the class $english = new Dictionary; // creating new object using the class called Dictionary $english->type = "EN"; // adding a value to the 'type' property located within the dictionary class $english->translations['TREE'] = "branch"; //adding TREE key and value branch to the translations array $english->translations['TREEtwo'] = "trunk"; //adding TREEtwo key and value trunk to the translations array $french = new Dictionary; //creating a new instance of the Dictionary object $french->type = "FR"; $french->translations['TREE'] = "arbre"; print_r( $english ); echo "<br/>"; print $english->summarize(); //prints the summarize method ?> class_dictionary.php <?php class Dictionary { //new class public $translations = array(); private $type =" "; //new variables called 'PROPERTIES' public setType($type) { $this->type = $type; } public getType() { return $this->type; } function summarize() { //function within a class is called a 'METHOD' $ret = "Dictionary type: {$this->type}\n"; // {$this->type} means: use $type which is found within THIS class $ret .= "Terms: ".count( $this->translations )."\n"; // {$this->type} means: count the length of the $translation array which is found within THIS class return $ret; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/260579-please-could-you-comment-on-my-comments-oop-beginner/#findComment-1335596 Share on other sites More sharing options...
trq Posted April 9, 2012 Share Posted April 9, 2012 Haha. My fault. Your missing the function keyword (as was I in my original example). public function setType($type) { $this->type = $type; } public function getType() { return $this->type; } Quote Link to comment https://forums.phpfreaks.com/topic/260579-please-could-you-comment-on-my-comments-oop-beginner/#findComment-1335598 Share on other sites More sharing options...
wright67uk Posted April 9, 2012 Author Share Posted April 9, 2012 thank you... I've got the functions in place - I should of known better myself! now when i run dictionary.php i get; Fatal error: Cannot access private property Dictionary::$type in C:\wamp\www\dictionary.php on line 6 Quote Link to comment https://forums.phpfreaks.com/topic/260579-please-could-you-comment-on-my-comments-oop-beginner/#findComment-1335603 Share on other sites More sharing options...
trq Posted April 9, 2012 Share Posted April 9, 2012 That is because you cannot access the $type property directly anymore, you need to access it via the new methods you have created. May I suggest you take a look at the examples in the manual? This is all pretty straight forward stuff. http://php.net/oop5 Quote Link to comment https://forums.phpfreaks.com/topic/260579-please-could-you-comment-on-my-comments-oop-beginner/#findComment-1335620 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.