You need to instantiate (create) $newVar outside of the function first. Then it will be view-able by your other function. You see, scope determines what objects can be seen other objects. If you create a variable within a function, it will only be usable from within that function.
A constructor allows you to initialize an object's properties upon creation of the object. If you create a __construct() function, PHP will automatically call this function when you create an object from a class. Notice that the construct function starts with two underscores (__)!
The var keyword in PHP is used to declare a property or variable of class which is public by default. The var keyword is same as public when declaring variables or property of a class.
PHP Function Arguments Information can be passed to functions through arguments. An argument is just like a variable. Arguments are specified after the function name, inside the parentheses. You can add as many arguments as you want, just separate them with a comma.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With