HA: Re: [PHP-DEV] Re: Pre proposal for "Class extension functions"
| From: | Andrey O Gromov | Date: | Thu, 27 Sep 2018 15:15:50 +0000 |
| Subject: | HA: Re: [PHP-DEV] Re: Pre proposal for "Class extension functions" | ||
| References: | 1 2 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-103270@lists.php.net to get a copy of this message | ||
I think that it is good idea to write page on RFC wiki with detailed
descriptions, examples and motivations. I see that it can't be accepted in
nearest future, but let it be for history. :)
There is many open questions. I will try to resolve that I can.
>>> Bring a little Javascript into PHP?
>> In fact, I looked at Kotlin :)
> Kotlin does not modify classes.
> This sort of thing is sound in compiled languages like Kotlin and C#,
where
> this is really syntactic sugar for calling user-defined functions with
> object-like syntax - it's possible (and sound) *only* because these
> languages can resolve such calls at compile-time.
Yes, I understand it. At the beginning I'm trying to find way for Kotlin
style realization - function with receiver. But, unfortunately, it seems
almost impossible if keep it simple.
> where this is really syntactic sugar
Exactly. I'm positioning this as syntactic sugar. And very useful sugar I
hope.
> In dynamic languages like JavaScript, you're literally writing to a
> map of methods, which means execution order is critical, and overwrites
are possible
Need some advice. Why execution order is critical?
Inserting into
ce->functions_list maked at compile time, and overwtites
will be checked at this time too.
> What's wrong with just calling a function and passing the object as
argument?
Nothing but aesthetic sense.
For example, PDO.
Classic way:
$stmt = $db->prepare($sql);
$stmt->execute();
$oneValue = $stmt->fetch(PDO::FETCH_NUMERIC)[0]??$default;
Standart function way
function takeOne($db, $sql, $default){
//see "classic way"
return $value;
}
$oneValue = takeOne($db, $sql, $default);
This is looks not good for me.
- 3 arguments
- no intuitive connection to $db
- "takeOne" placed in global functions scope for simple utility logic.
Proxy class method. Let me do not write monstrous wrapper over PDO?
Extension function style
function PDO->takeOne($sql, $default){
//see "classic way"
return $value;
}
$oneValue = $db->takeOne($sql, $default);
- Intuitive connection to $db.
- "takeOne" encapsulated inside PDO class