I'm not able to get the arguments passed onto a function in the constructor. Let me a bit clear with the code snippet below.
class Foo{
public function __construct($arg){
$arg = $arg1;
}
public function A($arg1){
echo $arg1;
}
}
$obj = new Foo();
$data = $obj->A('hello');
Here, the call is being made to the function A with an argument. I'm not able to get the same argument value in the constructor. I'm just giving an idea of my code.
Some additional examples:
Constructor Argument
class Foo{
public function __construct($argument){
echo $argument;
}
}
new Foo('Hello World');
Constructor Argument + Set argument to object as property
class Foo
{
public $argument = '';
public function __construct($argument){
$this->argument = $argument;
}
public function printIt()
{
echo $this->argument;
}
}
$foo = new Foo('Hello World');
$foo->printIt();
In your _construct
, $arg1
is not available for use, since it doesn't come from anywhere. This code would work fine if you would just remove $arg = $arg1;
and remove $arg
from the parameters of your constructor.
Right now the constructor is trying to pass a value from a variable that doesn't exist on that scope to $arg
.
<?php
class Foo {
private $arg1;
public function __construct($arg) {
$this->arg1 = $arg;
}
public function A() {
echo $this->arg1;
}
}
$obj = new Foo("hello");
$data = $obj->A();
try this, it will work