Q: How do I do an overloaded constructor in PHP?
A: You don’t need them
Consider the following
var $name ;
# if the default ctor is invoked, name
# this instance "unnamed"
$this->name = 'unnamed' ;
# Fine and dandy, but what if
# we want to supply a name
# in an overloaded ctor?
function __construct( $iname )
$this->name = $iname ; #doesn't work
# because overloaded ctors of this style
# aren't supported in PHP!!
OK. Well I promised in the title of this post that you didn’t need overloaded constructors in php Here’s why.
Redefine class Foo as follows:
var $name ;
# specify a single ctor, WITH DEFAULT VALUES
# FOR PARAMETERS. This is equivalent, and in my
# opinion a better syntax
# for getting overloaded constructors to work.
function __construct( $iname = 'unnamed' )
$this->name = $iname ;
There. Fewer lines of code, less mess, same effect. This was an excellent design decision by the php group.
installing php pear extensions
Well, i always forget how this is done, but its actually really easy.
- Navigate to your php install directory. for me, i go start-> run -> cmd.exe
You PHP install dir may be different. If you used xampp, its probably C:\xampp\php. The default location of Apache 2.2 if you install it by itself is something like c:\program files\apache software foundation\apache2.2\php
- Then at that command prompt, say I want to add the Math_Numerical_RootFinding PEAR package to my php install:
C:\Apache2.2\php>pear install Math_Numerical_RootFinding
And that’s it! PHP automatically resolves the network connection and downloads the latest build of the package.
IF the package you’re trying to get is still in BETA (as some of the good ones seem to be stuck in a perpetual beta), then you would have to pay attention to the error message that appears when you try to install it.
So for example, I wanted the Math_Polynomial package, so I try:
pear install Math_Polynomial
But it comes back at me with
Failed to download pear/Math_Polynomial within preferred state “stable”, latest release is version 0.1.0, stability “beta”, use “channel://pear.php.net/Math_Polynomial-0.1.0″ to install
Cannot initialize ‘channel://pear.php.net/Math_Polynomial’, invalid or missing package file
Package “channel://pear.php.net/Math_Polynomial” is not valid
So you have to do:
pear install Math_Polynomial-0.1.0