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
I want to call a class and pass a value to it so it can be used inside that class
The concept is called "constructor".
As the other answers point out, you should use the unified constructor syntax (__construct()
) as of PHP 5. Here is an example of how this looks like:
class Foo {
function __construct($init_parameter) {
$this->some_parameter = $init_parameter;
}
}
// in code:
$foo = new Foo("some init value");
Notice - There are so-called old style constructors that you might run into in legacy code. They look like this:
class Foo {
function Foo($init_parameter) {
$this->some_parameter = $init_parameter;
}
}
This form is officially deprecated as of PHP 7 and you should no longer use it for new code.
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.
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))
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 an 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.
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.
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);
}
}
Wow I cannot believe the answers here! They will all work but are wrong, the right way of set the variables is by a getter and setter, this allows you to set your variable neatly and perform checks etc on them. e.g.
class myClass {
private $myVar;
public function set_var($var) { // You can then perform check on the data etc here
$this->myVar = $var;
}
function __construct() { // PHP5 constructor
}
public function do_something() {
echo "blah";
}
}
What this allows you to do is call the object correctly e.g.
$objNew = new myClass();
$objNew->set_var("Set my variable");
$objNew->do_something();
This is the tidy way of doing it and on large projects and scripts you will be glad of this, I am having this problem right now with some-else's script which cannot be updated easily because it has been written in the other ways mentioned in this page.
It also allows you to have an unlimited number of variables for the class with out a silly call to the object e.g.
$objNew = new myClass("var1","var1","var1","var1","var1","var1","var1","var1","var1","var1","var1","var1","var1","var1","var1","var1","var1");
This is basically no clearer than using a function.