Re: [RFC] User Defined Operator Overloads (v0.6)

From: Date: Wed, 15 Dec 2021 21:18:14 +0000
Subject: Re: [RFC] User Defined Operator Overloads (v0.6)
References: 1 2 3  Groups: php.internals 
Request: Send a blank email to internals+get-116651@lists.php.net to get a copy of this message
On Wed, Dec 15, 2021, at 10:10 AM, Rowan Tommins wrote:
> On 15/12/2021 15:03, Dik Takken wrote:
>> $query->where(Price < 100);
>>
>> Here Price is a class that represents a database column which has a 
>> (static) overload of the '<' operator. The operator overload yields an 
>> object representing a database expression, which gets passed to the 
>> where() method. 
>
>
> The biggest problem with this particular example is not the operator 
> overloading, but the bare word "Price", which is currently a constant 
> lookup, not a class reference, as in:
>
> const Price = 50;
> var_dump(Price < 100);
>
>
> However, with any version of operator overloading that didn't limit the 
> return values of the overloaded operator, you could do something like:
>
> $query->where(Product::$price < 100)
>
> Where the static property Product::$price is an object which overloads 
> the "<" operator to return some kind of Condition object which can be 
> used by the query builder.

Cool as that would be, it poses a problem as it would mean the Price object could either be directly
compariable, or query-builder-comparable, but not both.  There's no way to have multiple
<=> overrides in different contexts.

Also, for <=> in particular, that one is restricted to only return -1 | 0 | 1 anyway, so it
wouldn't be able to return a query builder object.

--Larry Garfield


Thread (45 messages)

« previous php.internals (#116651) next »