Ok here we go:
Code: Select all
class A {
var $myVar1;
var $myVar2;
function A(){ //class constructor - will be run when you initiate an instance of the classs
$this->myVar1 = "hello";
$this->myVar2 = " world";
}
function print(){
echo $this->myVar1.$this->myVar2;
}
}
class B extends A {
function B($var1, $var2){
$this->myVar1 = $var1;
$this->myVar2 = $var2;
}
}
Those are the classes, now we call them.
Code: Select all
$myClass = new A();
$myClass->print();
"hello world" is printed to thes screen, as we call class A, it runs the constructor and places "hello" and " world" in the classes vars.
Code: Select all
$myClass = new B("Hello", " Johnson");
$myClass->print();
This time "Hello Johnson" is printed, as in class B you have to pass some initial vars to the class constructor which get put into the class vars.
Code: Select all
$myClass = new B("Hello", " Johnson");
$myClass->print();
$myClass2 = new B("Hello", " Billy");
$myClass2->print();
Now we call two instances of the B class and give them different parameters, they then print what they are given - so "Hello JohnsonHello Billy" is printed since we didnt put a break betweet the two calls.
The two classes we initalise DONT share the same actual variables, but both have access to the variables that were declaired in the parent class or the class itself.
Umm...A class is just a template that you can call instances of. When you do (by doing $blah = new Class();) you provide space in the computers memory for any variables - called properties - in the class and parent classes. By extending from another class you inherit the parents class methods and properties, which is why you can use the parents methods and properties.
Hope this helped abit.