views:

2164

answers:

8

So here is the deal. I want to call a class and pass a value to it so it can be used inside that class in all the various functions ect. ect. How would I go about doing that?

Thanks, Sam

+9  A: 

You refer to what is called a constructor.

class Foo {
    function Foo($init_parameter) {
        $this->some_parameter = $init_parameter;
    }
}

// in code:

$foo = new Foo("some init value");
Tomalak
Thanks dude, worked perfectly.
I love you more than words can describe.
You probably should call the method __construct() rather than Foo, as that's the recommended behaviour in PHP5
Ciaran McNulty
Hm... But did the OP mention the PHP version he uses?
Tomalak
php 4 was discontinued over a year ago.
troelskn
+7  A: 

In new versions of PHP (5 and up), the function __constuct is called whenever you use "new {Object}", so if you want to pass data into the object, add parameters to the construct function and then call

$obj = new Object($some, $parameters);

class Object {
    function __construct($one, $two) {}
}

Named constructors are being phased out of PHP in favor of the __construct method.

davethegr8
+1 for the deprecated warning - didn't know that. But as I see PHP it is not before version 7 that they actually *do* it.
Tomalak
+2  A: 

You can do this like that:

class SomeClass
{
   var $someVar;
   function SomeClass($yourValue)
   {
       $this->someVar = $yourValue;
   }

   function SomeFunction()
   {
       return 2 * $this->someVar;
   }
}

or you can use __construct instead of SomeClass for constructor in php5.

empi
A: 

This is how I do mine

class MyClass{

       public variable;//just declaring my variables first (becomes 5)
       public variable2;//the constructor will assign values to these(becomes 6)

       function__construct($x,$y){
            $this->variable  = $x;
            $this->variable2 = $y;
       }

       function add(){

            $sum = $this->variable + $this->variable2
            return $sum;

       }

}//end of MyClass class

create and instance and then call the function add

$myob = new MyClass(5,6);//pass value to the construct function

echo $myob->add();

11 will be written to the page not a very useful example because you would prefer to pass value to add when you called it but this illustrates the point.

andrew
A: 

These answers are super sweet! I'm glad I found this!!!!

Len Malcom
A: 
class SomeClass
{
    public $someVar;
    public $otherVar;

    public function __construct()
    {
        $arguments = func_get_args();

        if(!empty($arguments))
            foreach($arguments[0] as $key => $property)
                if(property_exists($this, $key))
                    $this->{$key} = $property;
    }
}

$someClass = new SomeClass(array('someVar' => 'blah', 'otherVar' => 'blahblah'));
print $someClass->someVar;

This means less maintenance in the long run.

Order of passed variables is not important anymore, (no more writing defaults like 'null': someClass(null, null, true, false))

Adding a new variable is less hassle (don't have to write the assignment in the constructor)

When you look at the instantiation of the class you'll know immediately what the passed in variables relate to:

Person(null, null, true, false)

vs

Person(array('isFat' => true, 'canRunFast' => false))

M.B
A: 

Think everyone's missing the obvious here. Yes, the PHP4 constructor is deprecated, but you can write your class to be backwards compatible like this:

class myClass {

    var $myVar;

    function myClass { // PHP4 constructor, calls PHP5 constructor
        $this->__construct();
    }

    function __construct() { // PHP5 constructor
        doSomeStuff($myVar);
    }
}
Chris Cox