Is it possible to overload operators in PHP?
If you are using PHP5 (and you should be), take a look at the SPL ArrayObject classes. The documentation isn't too good, but I think if you extend ArrayObject, you'd have your "fake" array.
EDIT: Here's my quick example; I'm afraid I don't have a valuable use case though:
class a extends ArrayObject {
public function offsetSet($i, $v) {
echo 'appending ' . $v;
parent::offsetSet($i, $v);
}
}
$a = new a;
$a[] = 1;
PHP overload = operator
No. PHP doesn't support operator overloading, with a few exceptions (as noted by @NikiC: "PHP supports overloading of some operators, like [], -> and (string) and also allows overloading some language constructs like foreach").Piskvor
php overload = operator
but in http://pecl.php.net/package/operator is a php extension that Operator overloading for: +, -, *, /, %, <<, >>, ., |, &, ^, ~, !, ++, --,
+=, -=, *=, /=, %=, <<=, >>=, .=, |=, &=, ^=, ~=,
==, !=, ===, !==, <, and <= operators.
Conditional support for > and >= available with application of a patch.
php overload = operator
No. PHP doesn't support operator overloading, with a few exceptions (as noted by @NikiC: "PHP supports overloading of some operators, like [], -> and (string) and also allows overloading some language constructs like foreach").
Is there an way to use Operator Overloading in PHP?
Well PHP doesn't support Operator Overloading altho I'd recommend looking at the thread bellow to read about possible similar solutions
Is it possible to overload operators in PHP?
But bare in mind, PHP's concept of overloading is no where similar to C++
Comparison operator overloading in php
http://pecl.php.net/package/operator
see this as well:
http://webreflection.blogspot.com/2008/06/from-future-php-javascript-like-number.html
Can I overload an array subscript operator in PHP?
Yes, with the ArrayAccess
interface. Although you won't be able to cast the object to a proper array.
With ArrayObject
(as mentioned in the duplicate question) you can however. Also see this comment on php.net on how to achieve this when you are extending ArrayObject
.
PHP static class Array[] operator?
[]
can not be overloaded only variable and methods
Sample Class
class MyStaticClass
{
static public $somthing = array("somthingElse"=>"Hello Benj") ;
}
Calling it directly
var_dump(MyStaticClass::$somthing["somthingElse"]);
You can also use
$MyStaticClass = MyStaticClass::$somthing ;
var_dump($MyStaticClass["somthingElse"]);
Both of them would Output
string 'Hello Benj' (length=10)
In PHP 5.4
function MyStaticClass()
{
return MyStaticClass::$somthing ;
}
var_dump(MyStaticClass()["somthingElse"]);
php overload equals-operator
Here's a neat little trick I recently found out:
class Foo {
public $a;
public $b;
public function __toString() {
return (string)$this->a;
}
public function __construct($a, $b) {
$this->a = $a;
$this->b = $b;
}
}
$a = new Foo(1, 'a');
$b = new Foo(2, 'b');
$c = new Foo(3, 'c');
$d = new Foo(2, 'd');
$array = array($a, $b);
$key = array_search($d, $array); // false
$key = array_search((string)$c, $array); // false
$key = array_search((string)$d, $array); // 1
This also works:
$is_equal = ((string)$d == $b); // true
When passed a string $needle, array_search
will try to cast the objects contained in $haystack to string to compare them, by calling the __toString
magic method if it exists, which in this case returns Foo::$a
.
Related Topics
PHP Expression ≪≪≪Eob
How to Re-Index the Values of an Array in PHP
Laravel 5 Failed Opening Required Bootstrap/../Vendor/Autoload.PHP
Remove Warning Messages in PHP
How to Linkify Urls in a String With PHP
Converting a Simplexml Object to an Array
Calculating Days of Week Given a Week Number
PHP Fatal Error Failed Opening Required File
Get the Date of Next Monday, Tuesday, etc
How to Get Enum Possible Values in a MySQL Database
Laravel Update Model With Unique Validation Rule For Attribute
How to Store File Name in Database, With Other Info While Uploading Image to Server Using PHP
How to Best Store User Information and User Login and Password