I am getting started with OO PHP, and after reviewing a variety of classes which I have downloaded from the internet, I have noticed that some – but not all – of these classes have an intial function with the same name e.g.
class MyClass{
function MyClass{
//function contents in here
}
function otherfunction{
//more stuff here
}
}
What is this inital function for? And how does it help with writing classes?
It’s an old-style constructor. If you are using PHP 5 (you ought to), you should avoid those constructors and do instead:
Constructors are, in short, used to run initialization code and enforce class invariants.