Even More PHP Learning: Codecacademy


At this point, I'm pretty much done with the Beginning PHP5 book.  The next chapter was on XML, then the rest was DB stuff and OO programming.  Decided it was time to move on to something at least somewhat more up-to-date.  So I decided to go through some of the PHP stuff at Codecacademy (free).  I have to say, the interface they use for their lessons is pretty nifty and the checking of your work seems to mostly work. Sometimes it allows you to be a little flexible, while other times not so much.  Anyway, you can see my profile on there if you're bored.

I started with the functions section, which covers built-in functions as well as writing your own.  Since it didn't really hit on anything new, I won't bother repeating stuff from that there.  Then I got into the Objects in PHP which got my feet wet in Classes.  Essentially, classes are like what we call Beans in our Mach-II apps, at least as far as I can tell so far: an object with a collection of properties and methods.

After going through the basics of classes, i.e. creating and instantiating (and that whole damn arrow notation thing), the first special method covered is the constructor method.  Kind of like an init() function – it's called automatically when you create a new instance of a class.

<?php
  class Person {
	 public $isAlive = true;
	 public $firstname;
	 public $lastname;
	 public $age;
	 
	 public function __construct($firstname, $lastname, $age) {
		$this->firstname = $firstname;
		$this->lastname = $lastname;
		$this->age = $age;
	 }
  }
  
  $teacher = new Person("boring", "12345", 12345);
  $student = new Person("Summer", "Wilson", 37);
  
  echo $student->age;
?>

It then did a quick reminder on doing a regular method in a class (much like a function).  One thing I really like is that after it shows you a bit with hand holding, it basically says "okay, now take you learned and try doing this" with some set task to perform.  Yeah, the examples are all basic, but that's fine.

Anyway, for the final bit, it had me make a dog class with some methods. 

<?php
   class Dog {
	  public $numLegs = 4;
	  public $name;
	  
	  public function __construct($name){
		 $this->name = $name;   
	  }
	  
	  public function greet(){
	   return "Hello, my name is " . $this->name . " and I am a dog!";   
	  }
	  
	  public function bark(){
		 return "Woof!";
	  }
   }
   
   $dog1 = new Dog("Barker");
   $dog2 = new Dog("Amigo");
   
   echo $dog1->bark();
   echo $dog2->greet();
?>

Then, I had to do a cat class too, but with even less guidance than on the dog one.

<?php
	class Cat {
	  public $isAlive = true;
	  public $numLegs = 4;
	  public $name;
	 
	  public function __construct($name){
		 $this->name = $name;
	  }
	  
	  public function meow() {
		 return "Meow meow";
	  }
	}
	
	$cat1 = new Cat("CodeCat");
	echo $cat1->meow();
?>

The next lesson delved further into OO with discussions on things like inheritance as well as some of the built-in functions php has for looking at objects/classes.  Yay!  Functions noted include is_a, property_exists, and method_exists.  Seem pretty straightforward and do what you would probably expect from the names.

  • is_a(object, "classname") – is the indicated object of the indicated class
  • property_exists(object, "propertyname") – does the indicated object have a property with the indicated name
  • method_exists(object, "methodname") – does the indicated object have a method with the indicated name

And now we have inheritance – a class can extend an existing class, essentially giving it the same properties/methods as it's "parent".  You can also override the parents method, as the child's always take precedence, unless the parent doesn't allow functions to be overridden with the final keyword in the parent.  If a function cannot be overridden, PHP will throw a fatal error when you try.

<?php
   class Vehicle {
	public function honk() {
	  return "HONK HONK!";
	}
   }
   // Add your code below!
   class Bicycle extends Vehicle {
	  public function honk(){
		 return "Beep beep!";   
	  }
   }
   
   $bicycle = new Bicycle();
   echo $bicycle->honk();
?>

This section also talks about class constants – which I kind of got a look at in my earlier skimming of class stuff.  If a variable is prefixed with const in a class, it can't be changed.  Constants are accessed using the scope resolution operator, rather than the usual properties way.  Why?  Who knows…it does have the benefit of being accessible without creating an instance of the class though.  Likewise, static properties and functions can be accessed the same way.

<?php
	class Ninja {
		// Add your code here...
		const stealth = "MAXIMUM";
	}
	// ...and here!
		echo Ninja::stealth;
?>

And now for the final test – putting all that together in the end of lesson exercise, which is pretty basic.

<?php
	class Person{
		public static function say(){
			echo "Here are my thoughts!";   
		}
	}
	class Blogger extends Person{
		const cats = 50;   
	}
	
	Blogger::say();
	echo Blogger::cats;
?>

For completeness, I'm going to go ahead and go through the other 7 lessons, but since they cover more basic things that I've already blogged about here once (or twice…or thrice), I won't post about those unless I find something new. Alas, this course seems to be newer so noting else really heavy covered yet, but at least I feel I have a firmer understanding of this whole class thing.