Help banging out some concepts?!?!?!

Discussion in 'PHP' started by lazerorca, Nov 4, 2014.

  1. #1
    So I am new to php, and I have just started to tackle object orientation... it's a bit rough lol

    1. When is it best to use a static modifier for a class variable or method and when is it best to use a variable or method for an instance of a class... this hasn't been made very clear to me. I am not really super clear on what a class and an object is... I have an idea that an object is an instance of a class where john and jane would be instances of a name, but I'm not sure how to define something for a class or an instance of a class. It seems to me that you would define a class and the objects would just be a variation of them.

    2. When should something be defined as a class of its own? That is something that is perplexing me a bit.
     
    lazerorca, Nov 4, 2014 IP
  2. lazerorca

    lazerorca Peon

    Messages:
    14
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    1
    #2
    :-( I know someone out there in the internet abyss can help me :-P
     
    lazerorca, Nov 5, 2014 IP
  3. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #3
    Use static methods for a method that doesn't share properties of the class. Something like a class for formatting or sanitizing or validating is often best kept static since you're accessing individual methods without respect to the class itself. Basically, if you just use a method like a normal function, static is probably appropriate.

    Other main reason would be when using a singleton design pattern which the pros and cons of have been discussed to no end, so I'll leave you to research that yourself if desired.

    Fundamentally, anything that can be defined on its own should be its own class, nouns are classes. I like to use the car analogy. A car is a class, the color is a property, starting the car or moving a direction are methods, but an engine or driver would also be its own class.

    Sort of applying to your person example.
    
    class person {
    
        public $name;
        public $gender;
        public $age;
        public $weight;
        private $pets = array();
    
        public function __construct($name, $gender, $age, $weight) {
            $this->name = $name;
            $this->gender = $gender;
            $this->age = $age;
            $this->weight = $weight;
        }
    
        public function eat($amount) {
            $this->weight = $this->weight + $amount;
        }
    
        public function buyPet($pet) {
            $this->pets[] = $pet;
        }
    
    }
    
    class pet {
    
        public $type;
        public $name;
        public $temperment;
    
        public function __construct($type, $name, $temperment) {
            $this->type = $type;
            $this->name = $name;
            $this->temperment = $temperment;
        }
    
    }
    
    $john = new person('John', 'Male', 45, 178);
    $john->eat(3);
    
    $fluffy = new pet('Cat', 'Fluffy', 'Cuddly');
    
    $john->buyPet($fluffy);
    
    echo "<pre>";
    print_r($john);
    echo "</pre>";
    
    /* OUTPUT
    person Object
    (
        [name] => John
        [gender] => Male
        [age] => 45
        [weight] => 181
        [pets:person:private] => Array
            (
                [0] => pet Object
                    (
                        [type] => Cat
                        [name] => Fluffy
                        [temperment] => Cuddly
                    )
    
            )
    
    ) */
    
    Code (markup):
     
    Last edited: Nov 5, 2014
    jestep, Nov 5, 2014 IP
  4. lazerorca

    lazerorca Peon

    Messages:
    14
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    1
    #4
    Total newb questions... I am sorry. I know that I could probably find out all of this through reading, but I learn much better when I encounter a problem and then solve it as I come across them instead of reading and learning it all at once.

    cool... so my next question is... what is the advantage of using a static method over just a regular function that you would define in something like a functions.php file?

    this brings up a separate, unrelated question I have... with the __construct function... a method that is used in the ___construct function doesn't have to be defined before?
     
    lazerorca, Nov 5, 2014 IP