Results 1 to 4 of 4

Thread: classes - what are they?

  1. #1

    Thread Starter
    WiggleWiggle dclamp's Avatar
    Join Date
    Aug 2006
    Posts
    3,527

    classes - what are they?

    what are classes in php? How are they used. I have seen them, just dont understand how they work.

    what is "->" used for?
    My usual boring signature: Something

  2. #2
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Re: classes - what are they?

    The class is central to object orientated design methodology. In object orientated design you build a systems by putting together a set of objects. Much like a body contains many organs; each doing there own thing to accomplish a task and made to work together to form an organism.

    In object orientated design; an object usually represents a single entity within the system (i.e: an order). The object can execute its own precesses (methods) and communicate with other objects via its state (properties).

    A class is simply a template for an object. It describes how it behaves and how it should interact with other objects in a system. You use a class to create an instance of an object.
    PHP Code:
    class Human
    {
        const 
    GENDER_MALE 1;
        const 
    GENDER_FEMALE 2;

        private 
    $dob;
        private 
    $gender;
        private 
    $children = array ();
        private 
    $mother;
        private 
    $father;

        public function 
    __construct($gender)
        {

            
    $dob Now();
        
    $this->gender $gender;
        }

        public function 
    reproduce(Human $partner$noChildren)
        {
            if (
    $partner->getGender() == $this->gender()) {
                
    thrw new Exception('This version of humanity does not support same sex couples.');
            }

        if (
    $this->gender == self::GENDER_MALE) {
            
    $mother $partner;
            
    $father $this;
        } else {
            
    $mother $this;
            
    $father $partner;
        }

        for (
    $x 1$x<=$noChildren$x++) {
            
    $child = = new Human(rand(GENDER_MALEGENDER_FEMALE);
            
    $child->setMother($mother);
            
    $child->setFather($father);
                
    $this->children[] )
            }
        }

        public function 
    getGender()
        {
        return 
    $this-> gender;
        }

        public function 
    getMother()
        {
        return 
    $this->mother;
        }

        public function 
    getFather()
        {
        return 
    $this->father;
        }

        public function 
    setMother(Human $mother)
        {
        if (
    $mother->getGender() != self::GENDER_FEMALE) {
            throw new 
    Exception ('Gender Adnormality');
        }

        
    $this->mother $mother;
        }

        public function 
    setFather(Human $father)
        {
        if (
    $father->getGender() != self::GENDER_MALE) {
            throw new 
    Exception ('Gender Adnormality');
        }

        
    $this->father $father;
        }

        public function 
    getDOB()
        {
        return 
    $this->dob;
        }

        public function 
    getChildren()
        {
        return 
    $this->children;
        }

    The above example demonstrates a simple class called Human. Disregarding the simplistic and possibly biased view of humanity. The functions in the class represent methods reproduce() is a defined behaviour. The getXXXXX() methods are accessors/getters and allow you to access properties declared as private (private means only accessible within the object).

    -> is the object access operator $o->function() executes a method $o->property returns a public property. The :: operator is the scope resolution operator. This is used to access constant and static properties and methods.

    You create an instance of an object using the new operator:
    PHP Code:
    $male = new Human(Human::GENDER_MALE);
    $female =  new Human(Human::GENDER_FEMALE);

    $male->reproduce($female4); 
    The benefits of object orientated design are clear. Firstly you can encapsulate data and hence protect it inside an object by declaring it private. There are three others too which I let you read up on:
    • Encapsulation.
    • Polymorphism.
    • Inheritance.
    • Abstraction.

    OOP is the best thing since sliced bread. I recommend it to any web developer in conjunction with the MVC design pattern.
    Last edited by visualAd; Jul 27th, 2007 at 03:23 AM.
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  3. #3
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Re: classes - what are they?

    P.s: Read the PHP5 OOP tutorial in my sig
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  4. #4

    Thread Starter
    WiggleWiggle dclamp's Avatar
    Join Date
    Aug 2006
    Posts
    3,527

    Re: classes - what are they?

    wow. Ok, thanks va!
    My usual boring signature: Something

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width