I have a class as follows:
class Integer {
private $variable;
public function __construct($variable) {
$this->varaible = $variable;
}
// Works with string only
public function __isString() {
return $this->variable;
}
// Works only, If Im using the class as a function (i must use parenthesis)
public function __invoke() {
return $this->variable;
}
}
$int = new Integer($variable);
I would like work with class as with variable like:
$result = $int + 10;
I don´t known, how can I return $int;
?
PHP does not support overloading operators (which is the technical thing you're looking for). It doesn't know what to do with +
when one of the operands is a class Integer
, and there's no way to teach PHP what to do. The best you can do is implement appropriate methods:
class Integer {
..
public function add(Integer $int) {
return new Integer($this->variable + $int->variable);
}
}
$a = new Integer(1);
$b = new Integer(2);
echo $a->add($b);
Yes, see Example 4 of php's call_users_func() page;
<?php
class myclass {
static function say_hello()
{
echo "Hello!
";
}
}
$classname = "myclass";
call_user_func(array($classname, 'say_hello'));
call_user_func($classname .'::say_hello'); // As of 5.2.3
$myobject = new myclass();
call_user_func(array($myobject, 'say_hello'));
?>
public function __construct($variable) { $this->varaible = $variable; }
are this typo or not? on $this->varaible ?