sniko Posted March 16, 2010 Share Posted March 16, 2010 Creating a class can be fustrating but hopefully this will help. The basic class layout <?php class [ClassName] { function [FunctionName]() { //Things you want it to do in here } } $[VarName] = new [ClassName]; As you can see, we start a class by doing the following class [className] { } Then, inside the class, once we have created it, we need to add a function. We can do this by function [FunctionName]() { //Function Content } After, all of that is done, we can then actually create it, if you'd like to see it like that. We do this by doing $[VarName] = new [ClassName]; Now, to apply it to a page, we can do this. Bearing in mind, in the function we have already echo'ed it, we don't applying it to a page. $[VarName]->[FunctionName](); As an example, lets create a welcome message class (Totally useless, but it is what i used when i learnt) <?php class Welcome { function echoMe() { $do = 'hello'; echo $do; return $do; } } $welcome = new Welcome; $welcome->echoMe(); //This is what is displayed on the page, the Output being "hello" If you have any questions/queries please reply, also i have just learnt how to do classes, so, please bear with me if any of my replies are incorrect to you more experienced coders. Also, I'd love to see some of you attempting to create a class. Email: s.[email protected].uk (No spamming or signing me up to things) - Sniko Quote Link to comment Share on other sites More sharing options...
a_bertrand Posted March 17, 2010 Share Posted March 17, 2010 Honestly I don't use much PHP classes and tend to use a more procedural approach (beside for a few specific issues). Why? Because it simply miss all the needed features you would need as soon as you code in object oriented ways. Part of the issue come from the fact PHP don't check the type of a variable, and you can store anything anywhere, part of the problem is the lack of clear cut between references and data copy of a variable / object, and part of the problem is simply the lack of support for things supported by other languages. Said that, this one of the code where I do have classes: $terrainNames=array("G"=>"Grass","W"=>"Watter","F"=>"Fire","I"=>"Ice"); Class Card { var $select_other_on_place=false; var $select_placed_on_use=false; var $select_pool_on_use=false; var $select_other_on_use=false; var $can_defense=false; var $id=-1; var $userid=-1; var $other_id=-1; var $attack=0; var $defense=0; var $nbterrain=array("G"=>0,"W"=>0,"F"=>0,"I"=>0); var $hand; var $game; var $needToBeRemoved=false; var $cardName=""; var $roundUsed=false; function on_place() { return "$this->cardName as been placed. "; } function on_use() { return "$this->cardName as been used. "; } function on_defense($hp) { return ""; } function can_place() { return false; } function after_other_select($id) { return ""; } function after_select_placed($id) { return ""; } function attack($hp) { return "Attack for $hp"; } function description() { return $this->cardName." {$this->id}\n"; } } Class AttackCard extends Card { function on_place() { $this->can_defense=true; $this->hand->place($this); return "$cardName as been placed. "; } function on_defense($hp) { if($this->defense > $hp) { $this->defense-=$hp; return "$this->cardName absorbed $hp damage."; } else { $this->hand->hp-=$hp-$this->defense; $this->hand->remove($this); return $this->cardName." has been destroyed during the defense and made ".($hp-$this->defense)." to your health."; } } function on_use() { return attack($this->attack); } function can_place() { if($this->hand->terrains[$this->terrain] >= $this->nbterrain) return true; else return false; } function description() { return "Attack: {$this->attack}\nDeffence: {$this->defense}"; } } Class TerrainCard extends Card { function can_place() { return true; } function on_place() { foreach($this->nbterrain as $k => $v) { $this->hand->terrains[$k]+=$v; } $this->hand->remove($this); } function description() { global $terrainNames; $res=parent::description(); foreach($this->nbterrain as $k => $v) { if($v > 0) $res.="Terrain ".$terrainNames[$k]." $v\n"; } return $res; } } With such construct, I can use a card and don't care too much about what kind of card it is... (it's for an in game card game). Quote Link to comment Share on other sites More sharing options...
Danny696 Posted March 17, 2010 Share Posted March 17, 2010 sniko, whats the point in defining the class varabile in the same file? Quote Link to comment Share on other sites More sharing options...
sniko Posted March 17, 2010 Author Share Posted March 17, 2010 sniko, whats the point in defining the class varabile in the same file? Im not too sure, so you don't have to in other files? Quote Link to comment Share on other sites More sharing options...
Danny696 Posted March 17, 2010 Share Posted March 17, 2010 Lol, saw mtg do it ;P i suppose so, i prefer it in other files, just so i know where i am :S Quote Link to comment Share on other sites More sharing options...
Djkanna Posted March 18, 2010 Share Posted March 18, 2010 I don't like classes too much the only time I use a class is if I use a PHP Framework. Quote Link to comment Share on other sites More sharing options...
Guest Drizzle Posted March 22, 2010 Share Posted March 22, 2010 I only use classes when im creating websites from scratch and dont want to have to keep defining connection id variables in every query. Other than that i just do regular 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.