PHP Tutorial, Round 3: Classes


I'm guessing this will be another big topic, and it is certainly the first that is pretty much totally new to me for PHP stuff. Depending on how long/slow I go through this bit, I may break this up into multiple posts.  But for now…let's read the introduction.

Before reading that, I thought that classes were just an upgraded version of a function.  But apparently its closer to what we would consider a bean in our ColdFusion stuff, i.e. an object. The main difference is that while, for the most part, beans just have getters and setters, it seems like classes can (and usually do) have functions/methods that can do more?

So now to make my first class.

<?php
    class Pet {
        public $name;
        public $species;    
        public $breed;    
        public $age;
        public $sex;    
    
        public function getSummary() {
            return $this->name . " is a " . $this->age . " year old " . $this->species;
        }
    }
    
    $pet = new Pet();
    $pet->name = "Fusion";
    $pet->species = "cat";
    $pet->breed = "domestic medium hair";
    $pet->age = "13";
    $pet->sex = "female";
    
    echo $pet->getSummary();
?>

As a side note, Dreamweaver is being rather awesome with code helps with this stuff so far – while I was doing the later part of the code, ti was auto suggesting from the class stuff. Nice.  Anyway, that seems simple enough. next is constructors and destructors.  Special functions called when an object is created or destroyed. 

<?php
    class Pet {
        public $name;
        public $species;    
        public $breed;    
        public $age;
        public $sex;    
    
        public function getSummary() {
            return $this->name . " is a " . $this->age . " year old " . $this->species;
        }
        
        public function __construct($name) {
           $this->name = $name;
        }
        
        public function __destruct() {
          echo "Meow. Meow. Meow. Meeeeooooooowwwwwwwwwwwwwww...oh, were you sleeping?";
        }
    }
    
    $pet = new Pet("Fusion");
    
    echo "Hi. I'm " . $pet->name . "<br /><br />";
?>

The general concept sounds useful, though the examples seem fairly pointless.  Ah well, baby steps and all, right? Quick bit on private vs public vs protected classes. Similar enough to function privacy that it was easy to follow.  Now inheritance…one thing I've yet to see any use for even though ColdFusion can do it as well.  Again, baby steps…




<?php
    class Pet {
        public $name;
    
        public function getSummary() {
            return $this->name . " is a pet";
        }
    }
    
    $pet = new Pet();
    $pet->name = "Yuki";
    echo $pet->getSummary() . "<br /><br />";
    
    class Dog extends Pet { }
    
    $pet = new Dog();
    $pet->name = "Blue";
    echo $pet->getSummary() . "<br /><br />";     class Cat extends Pet {
        public function getSummary() {
            return $this->name . " is a cat";
        }    
    }
    
    $pet = new Cat();
    $pet->name = "Fusion";
    echo $pet->getSummary() . "<br /><br />";
?>

I understand it…presummably I'll find a use for it when I'm doing more real world stuff with it. *click*  Abstract classes….okay….so basically inherited classes that cannot themselves be used?  I think…mostly…

<?php
    abstract class Pet {
        public $name;
        public $breed;    
        public $age;
        public $sex;    
    
        public function getSummary() {
            return $this->name . " is " . $this->age . " years old ";
        }
        
        abstract public function mySound();
    }
    
    class Dog extends Pet { 
        public function getSummary() {
            return parent::getSummary() . " and is a dog";    
        }
        
        public function mySound() {
            return "* bark bark *";
        }
    }
    
    $pet = new Dog();
    $pet->name = "Blue";
    $pet->breed = "Austrlian Shepherd Mix";
    $pet->age = "10";
    $pet->sex = "female";
    
    echo $pet->getSummary() . "<br /><br />";
    echo $pet->mySound() . "<br /><br />";
?>

Easier to understand than I thought once I tried it, though I don't get why it's called abstract.  Conversely, we have static functions (or shared), so they can be used even if I don't actually make a class?

<?php
    class Pet {
        public $name;
        public $breed;    
        public $dateofbirth;
    
        public function getSummary() {
            return $this->name . " is " . $this->age . " years old ";
        }
        
        public static function CalculateAge($dateofbirth) {
            $birthDate = new DateTime($dateofbirth);
            $now = new DateTime();
            $interval = $now->diff($birthDate);
            return $interval->y;
        }
    }
    
    $dateofbirth = "1/10/2004";
    
    echo Pet::CalculateAge($dateofbirth) . " years old";
?>

I realized as I wrote this stuff the tutorial bits on data types never talked about dates.  Why not? Skimming on, it looks like it won't be covering dates at all.  Well pooh.  Oh well, I can (and did) Google.  On to constants…so how is this different from a static variable? Or can static variables be changed while constant ones can't?  Not going to bother doing an example for that since it's just "here it is".

And the last bit on classes, which went much faster than I thought, is on the final keyword.  So while abstract is essentially "can only be inherited" using the final keyword says "don't let anyone inherit.".  And it works on both classes and functions.  Okay.

When I pick back up on Monday, the tutorial moves into DB stuff. Yay!  Finally.  🙂

(BTW, if you're following along with the tutorial, you've probably noticed I'm doing my own versions of stuff versus just following the example as is; helps it stick better 😉 ). 

Note: I've realized that there is a bug in the SyntaxHighlighter causing it to blank out the BR tags from my code.  So if you see echo "" it is supposed to be echo "<br /><br />" and many of the other blank "" at in the concatenated strings are actually "<br />".  So far I haven't found a fix as the code, and while this bug was report on the developer's GitHUb 2 years ago, he has never responded.