[PHP] OOP PHP? Is it really worth it?

Indeed, code looks good.

I took a quick look at the zend framework. Looks good and usefull :), but it always takes ages for me to find out all functions and use them. Thats why I often write it myself and modify it to the project's needs, that way I learn the most but the code won't be as optimized as professional code like the Zend Framework.

Even if its not open-source its good to keep it organised ;). Because after a few months you probably forgot what some functions/pieces are about :P.


Yea, I always leave comments in the form like FragFrog does, and I require anyone who wishes to work on a project with me to do the same. And as I said, even if its something I am doing for my self for the hell of it, even if im only going to use it once, I keep the code clean and organized :)

And yea, Zend Framework does look useful, though I am not sure if I am going to start using it quite yet (I find its better practice if I write my own code).
 
PHP don't got pointers, but you're able to pass stuff along as refferences, which is the most common use of pointers in C (due to the lack of typesafe custom types).

To clear it up, I wasn't talking about pointers in php. I said, pointers as a concept is generally hard for people to grasp when they are learning comparing that to abstractions which is also a difficult concept.

As for the Zend Framework, I haven't used that yet. Nor looked at it, is it part of the Zend Studio?


Anyway interesting Thread, I don't see many here in this section that I comment on, (I don't really like to fix other ppl's code problems but some find that a challenge) as GriffinHeart said so little of your time is spent coding, so much more of my time is spent talking about it, and I love to talk. :P
 
And yea, Zend Framework does look useful, though I am not sure if I am going to start using it quite yet (I find its better practice if I write my own code).

Exactly what I think about it :D

As for the Zend Framework, I haven't used that yet. Nor looked at it, is it part of the Zend Studio?

Anyway interesting Thread, I don't see many here in this section that I comment on, (I don't really like to fix other ppl's code problems but some find that a challenge) as GriffinHeart said so little of your time is spent coding, so much more of my time is spent talking about it, and I love to talk. :P

You can get the framework for free. Just google it and you will find the download link. Its not packed with anything else as far as I know.

Yeah nice thread :), aren't much like these, I'm mainly active in this coders section :). And yeah, when some habboguy comes with crappy code, I generally leave it behind ^^,
 
To clear it up, I wasn't talking about pointers in php. I said, pointers as a concept is generally hard for people to grasp when they are learning comparing that to abstractions which is also a difficult concept.

As for the Zend Framework, I haven't used that yet. Nor looked at it, is it part of the Zend Studio?


Anyway interesting Thread, I don't see many here in this section that I comment on, (I don't really like to fix other ppl's code problems but some find that a challenge) as GriffinHeart said so little of your time is spent coding, so much more of my time is spent talking about it, and I love to talk. :P

Yea, I didnt really see another post like this one, and since I was thinking of going to OOP I thought I would make the thread and see what others had to say about it :)

And yea, I dont really like fixing other peoples code, better to discuss.
 
OOP is just much better for the structure of your script, also if your team mates have to take a look at your script, and you would have used OOP, then they will be able to edit it and work with it easylie since it haves a good clean structure.
 
pffftt....tbh most powerfull stuff in oop is polymorphism and agreggations/composition :P


i'd vote this for a stickie since its an usefull thread, with lots of explanations from different sources about what is oop and what is it good for

I agree, this is a useful thread - helped me quite a bit.
 
Added it in the usefull threads sticky here :wink:

Griffin, you said polymorphism and agreggations/composition are the most usefull aspects of OOP, could you perhaps clearify that a bit? I have a rough idea of polymorphism (mostly thanks to my C++ doc) but I'm not even sure what you mean with agreggations/composition.
 
OOP is worth it if u have the time and know what u are doing the scripts load faster u can even go test it yourself also oop is good for large projects because u can use a class / object over and over again insted of writting new functions for eatch instance
 
Abstraction. Clean.

Got bored. Got carried away. Wrote this from scratch. In this little box. Didn't test it. I wouldn't anyway. It needs fixing up. (Specifically EffectArea should use a message system, but then again if you use PHP you could just eval it). (Also would use an ID based system instead of array pushing). (References might be incorrect).

Well basically the point is you keep large projects organized. There is too many advantages to list, infact. You keep similar info in Entity, and only info specific to monsters in Monsters, and info specific to players in Players (on that subject, you would have another class because NPCs share a lot with Players). As you can see by the variables and methods. You gain the ability to keep it easy to debug, and change where necessary without much difficulty. (ie change it to agility instead of dexterity, or add stats).

Anyway :drinks_no

PHP:
class Coords
{
      var $x = 0;
      var $y = 0;
}

class EntityManager
{
      var $list = array();

      public function Add($entity)
      {
            $this->list[] = $entity;
      }
 
      public function Remove()
      {
            
      }

      public function GetListByArea($min, $max)
      {
            var $list = array();

            foreach ($this->list as &$entity)
            {
                  if($entity->coords->x > $min->x
                  && $entity->coords->x < $max->x
                  && $entity->coords->y > $min->y
                  && $entity->coords->y < $max->y)
                             $list[] = $entity;
          }

          return &$list;
      }

      public function EffectArea($effects, $min, $max)
      {
            var $list = $this->GetListByArea($min, $max);
 
            var $effectsSplit = explode("|", $effects);
 
            foreach ($list as &$entity)
            {
                  foreach ($effectsSplit as &$effect)
                  {
                        switch($effect)
                        {
                              case "flee": $entity->Flee();
                              break;
                              case "kill": $entity->Kill();
                              break;
                              case "torture": $entity->Torture();
                              break;
                              case "burn": $entity->Burn();
                              break;
                              case "explode": $entity->Explode();
                              break;
                        }
                  }
            }
      }
}

class NPCManager extends EntityManager
  {
      function NPCManager()
       {
  
       }
  }

class MonsterManager extends EntityManager
{
     function MonsterManager()
     {

     }

     //specific stuff
}

class PlayerManager extends EntityManager
 {
      function PlayerManager()
      {
 
      }

      public function SendAnnouncement()
      {
             foreach($list as  &$player)
                    $player->ShowAnnouncement("Hello World, this is your GM.");
      }

     //specific stuff
 }

class Entity
{
     var $coords = new Coords();

     var $level;
     var $health;
     var $mana;
     var $stamina;
     var $dexterity;
     var $strength;

     public function UpdateCoords($x, $y)
     {
          $this->coords->x = $x;
          $this->coords->y = $y;
     }

     public function Kill()
     {
          $this->health = 0;
     }
}

class Player extends Entity
  {
       var $reputation;
       var $skillPoints;
       var $experience;
       var $professions;
       var $class;
  
       function Player($hp, $mp, $sta, $dex, $str)
       {
            $this->health = $hp;
            $this->mana = $mp;
            $this->stamina = $sta;
            $this->dexterity = $dex;
            $this->strength = $str;
       }

       public function UseSkill($skill)
       {
               switch($skill)
               {
                       case "Fireball":
                               //stuff here
                        break;
                       case "Holy Nova":
                               var $min = new Coords($this->player->x - 10, $this->player->y - 10);
                               var $max = new Coords($this->player->x + 10, $this->player->y + 10);

                               $MonsterManager->EffectArea("explode|kill", $min, $max);
                       break;
                       case "Energy Shield":
                                //stuff here
                         break;
               }
       }

       public function ShowAnnouncement($text)
       {
          //send text to table or w/e
       }

     //specific stuff
  }

class Monster extends Entity
{
     var $anger;

     function Monster($hp, $mp, $sta, $dex, $str)
     {
          $this->health = $hp;
          $this->mana = $mp;
          $this->stamina = $sta;
          $this->dexterity = $dex;
          $this->strength = $str;
     }

     //specific stuff
}

var $MonsterManager = new MonsterManager();

var $monster = new Monster(100, 100, 100, 10, 10);
$monster->UpdateCoords(0, 0);

echo $monster->health;

var $player = new Player(100, 100, 100, 10, 10);
$player->UpdateCoords(0, 0);

echo $player->strength;

$player->UseSkill("Holy Nova");

echo $monster->health;
 
Back