Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Men and women are pretty similar - a lot more similar than people think,
but there are some crucial differences. Some of the shared attributes are
things like eye colour, hair colour, height and weight. But there are a
couple of glaring differences between the two such as reproductive
organs, gender and chromosomes.
The way that inheritance works in classes is that you put as many
common items within one class and then extend that parent class for each
sub-class. Sound complicated? Let me clarify, we have a class of Person
that will be extended to create Man and Woman . There will be some specifics
Man
or
Woman
As you can see, we have used the word extends to show that Man is an
extension of the Person class. You can imagine this as the variables within
Person being copied to a new class called Man so they are available to use
if their privacy allows.
Privacy
What you'll see in the above code is words such as protected , public and
private , these are varying levels of privacy and dictate where these
attributes can be altered, or functions called:
Public - Can be called anywhere, outside of the class, inside the
class, or even in a child.
Protected - Can be called from only within the current class or any
children of that class (eg: gender can be altered by Person or Man )
Private - Can only be called by the containing class. (eg: alive can
only be altered by
Person
class Site {
public static function relativeTime
relativeTime(){
return 'Yesterday';
}
}
Site::relativeTime()
/*
If we had to instantiate the class we'd do
$site = new Site;
$site->relativeTime();
*/
When within a static function we can't use the $this notation, as that
refers to instantiated objects only, so we will want to use the keyword
self , this works very much the same as the parent keyword that is
Magic Methods
Sometimes referred to as Overloading, this is the instance by where you
can have a generic function that is called if you try to get or set a variable
that doesn't exist, or try to call a function that doesn't exist. There are 4
that I'll run through, and they're all very similar they are __get() , __set() ,
__call() and __callStatic() . Make sure that you declare all of these as
public.
die
die('Unknown variable.');
}
}
}
$d = new Demo;
// Set a non-existent variable
$d->test = 'Test Variable';
// Get what we just stored
echo $d->test;
// Get a non-existant variable
echo $d->testFail;
break
break;
default
default:
die
die('Unknown method.');
}
}
}
$d = new Demo;
// Set a non-existent variable
$d->settest('Test Variable');
// Get what we just stored
echo $d->gettest();
// Get a non-existant variable
echo $d->gettestFail();
The logic is exactly the same for __callStatic() . One thing to note is that I
have included the line return $this; within the set case, this will mean that
you can chain your sets: $d->settest('test')->settest2('test2')>settest3('test3');
Singletons
A singleton class is a class that should only be instantiated once and
never instantiated again, for example if you had a class for database
interfacing you would only want that instantiated once. This is normally
achieved by having a static variable within the class sometimes named
instance , and a static method called getInstance - this will check if and
instance exists, if it doesn't, it will create one, then return the instance:
class DemoSingleton {
private static $instance = NULL
NULL;
public static function getInstance
getInstance(){
if
if(is_null(self
self::$instance)){
self
self::$instance = new self
self();
}
return self
self::$instance;
}
}
$class = DemoSingleton::getInstance();
Autoloading
Autoloading is very nifty, and is used in a lot of large frameworks - it allows
you to load the file that a class is written in automatically when you try to
instantiate it. The advantage of this is that only classes that you want to
instantiate & use will be included in your page and will keep memory
usage down.
The best way to autoload a class is to use a global function called
spl_autoload_register() , there are two ways to use it - my favourite is using
an anonymous function, but this is only possible in PHP 5.3.0+. The other
way is a named function, both below do the same thing:
// Using a named function
function autoload_class
autoload_class($class){
include_once
include_once('includes/' . $class . '.class.php');
}
spl_autoload_register('autoload_class');
// Using an anonymous function
spl_autoload_register(function($class){
include_once
include_once('includes/' . $class . '.class.php')
});
These will both look in a directory called includes for a file with the same
name of the class you're trying to instantiate. So trying to instantiate a
class called Person would look to include a file called Person.class.php .
Final Thoughts
Object oriented programming is an amazing feature and is definitely
something that you should use, whether exploiting extensions and magic
methods or not. OO can go so much further than what I have explained
here, such as the use of Factories and abstract functions - which I've only
ever had the need to use once. For further reading take a browse through
the PHP Classes & Objects Manual.
Tag: PHP
Tweet
Top