Several problems with this code:
Code: Select all
<?php
class myClass {
$var prop="";
function myClass(&$prop="") {
$this->prop=$prop;
}
function Foo() {
if ($this->propї"address"]=="myaddress") {
dosmth();
}
}
}
....
$prop=array(
"address"=>"ssss",
"name"=>"sdsfdsf"
);
$x=new myClass(&$prop);
?>
First is the excessive use of '&'. You don't need it here '
$x=new myClass(&$prop);', as you have it in the function definition. Secondly, you don't really need it at all. Your passing it by reference, which takes time in PHP. Then, finally, when you are in the constructor, you do ' $this->prop=$prop;', which does the copy anyways. So simply remove all the '&' from your script, and it will work exactly the same way, except faster.
Remember, PHP does copy-on-write, which means if I do:
Code: Select all
function name ( $var )
{
echo $var;
}
$var = "really long string";
name($var);
PHP doesn't make a copy of $var in the function. No need. It only makes a copy if you actually write to the variable. Otherwise, it basically uses a reference. When you actually write to the variable, it will make a copy. This is for memory issues, etc. If you are passing object, pass them by reference implicitly. Otherwise, you are probably wasting time.
Next, passing an array to a function to replace a long parameter list is a poor mans technique for not using objects. We have objects in PHP. Use them.
Rather than use your array, which means code maintenance is much more difficult, etc, you an object.
Code: Select all
<?php
class myClass
{
var $prop;
function myClass( &$prop )
{
$this->prop =& $prop;
}
function Foo()
{
if ($this->prop->getAddress() == "myaddress") {
dosmth();
}
}
}
class Person
{
var $address;
var $name;
function Person () {}
function setAddress ( $address )
{
$this->address = $address;
}
function setName ( $name )
{
$this->name = $name;
}
function getAddress ()
{
return $this->address;
}
function getName ()
{
return $this->name;
}
}
$Person = new Person();
$Person->setAddress("Address");
$Person->setName("Jason Lotito");
$myClass = new myClass($Person);
?>
This is more or less what you want. Rather than pass an array that can change over time, you pass an object for which you control the interface.
Using an array instead of a long parameter list has several problems. First and foremost is that it takes even more work to pass the array than it does to use the long paramater list.
Secondly, it's usually an indication that your parameter list is just too long to begin with. A long paramter list is usually a sign that you don't know what you are doing (that you don't understand the problem). A good function should do one thing, and do it well. This rarely requires a lot of parameters. When it does, that's usually when you should go about making an object.
Thirdly, a long parameter list is usually a sign to a long function. Long functions are bad, because they do more than one thing. A function should be short and simple. A function that is too long can be broken up into smaller pieces.
my 2 cents