Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:104087 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 52022 invoked from network); 4 Feb 2019 02:36:02 -0000 Received: from unknown (HELO mail-pf1-f171.google.com) (209.85.210.171) by pb1.pair.com with SMTP; 4 Feb 2019 02:36:02 -0000 Received: by mail-pf1-f171.google.com with SMTP id u6so5892600pfh.11 for ; Sun, 03 Feb 2019 15:16:47 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=basereality-com.20150623.gappssmtp.com; s=20150623; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=peaJzeak94B/pBdHpp+vXCV585NKLuio4FeOILNF154=; b=Em2U3CH4Op5EWJM2KleIronNK3WAKgoahTy5qvwnXr6ZhUIW6HMRAcseuBpUdA6c/2 +2uUxfGu+uyaSiievmsNUIchL8HGNdPzUkCo1gYqCVhFd5Y6HUsmowPL5KYkyZLecVmB bcYKHn7oxYd+OTF/2t11ErhKYfB5HiQXlLQb1Gp1yEJYqqGPIRfCjmXykajXBu8236Rz qHms4zTp+W1h/OTms82FP9g3GRsKYXfQ/nr+ErdYLeMv4PLgvujHtG1BbIH0nvSJfy21 nvilfHJ/oQNdCWgr4ciGNqrpMtNaAPcCaMh9a4ogjJVR5qwhsbVaq3J1kRU6Cri5VxFT RvUA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=peaJzeak94B/pBdHpp+vXCV585NKLuio4FeOILNF154=; b=liPEd9FlF8pkGDAW8fYwa5VsyFXCipHpFOJB4V5gltV4UlvEfTWf5TBlCPC3zhjicF wRGrlqaouvdV3SKDoq7dQhIg4hS51P1EFAdJJAmzJN9DCgq9LVGKhjPePUU+6gWAwXHg kJv65EHZMtZcyRquBPG+V8p60QI+l6MuLYARlM4iVDUvI2bmijNlyGO8aJ5j1m1KS57K n2YFkqrbo1QfLlxPJrbRaP/xA4TIiNL++s/GGHU4Jnx27etK0LyZPsBcyyHOwRoBadVn SW5jVoOBI0DBV+wfwlJmTCRMytroZh8Gg+ifQZ5IDOluwcRn0c8rYzX2U8fnHl+gEsXI 3lQg== X-Gm-Message-State: AHQUAubssHY6JPu2mm60xvVlFbI/cjHxvfVHKOP3g9FaDcz6GwxZJHB6 nIiKN6aYSzXtspAUmbkKo8a/dCeVE88h1p3jwueZcQ== X-Google-Smtp-Source: AHgI3IaP1Kkv+94AAKub5LKOehHdM0eDEhq2R8rR5fWvbgQpclFFLfjDQ7XxslFyDBO1koRbjKE3p6+ZvoVFURvG8WA= X-Received: by 2002:a63:2a44:: with SMTP id q65mr9911317pgq.231.1549235806737; Sun, 03 Feb 2019 15:16:46 -0800 (PST) MIME-Version: 1.0 References: In-Reply-To: Date: Sun, 3 Feb 2019 23:16:35 +0000 Message-ID: To: David Rodrigues Cc: PHP Internals Content-Type: text/plain; charset="UTF-8" Subject: Re: [PHP-DEV] PHP 8: Method Overloading, The Ressurection From: Danack@basereality.com (Dan Ackroyd) On Sun, 3 Feb 2019 at 18:39, David Rodrigues wrote: > > Hello everybody! > > The biggest problem that I see about this topic is the loss of performance, Although performance impact might be a problem, my impression is that PHP has some problems supporting method overloading that don't exist in other languages. > So, what other languages does in cases like > that? Java, for instance. Java doesn't have type-juggling so they avoid quite a few problems with dynamic types. Marcos Passos wrote: > Java, for instance, throw a compile-time error saying that the > method signature is ambiguous. Java compiles all the code in a program before running it, which makes that solution possible. PHP doesn't and so has to make some decisions at run-time. As Christoph wrote: > overload function sum(int $a, int $b): int; > overload function sum(float $b, float $b): float; > > > Which function would sum(17.4, 42) call? That's the fundamental problem that would need addressing. I believe that to have a chance of passing, any method overloading RFC would need to have a really comprehensive answer that covers all of the edge cases. But in addition to that, a really strong argument for why method overloading is a good thing needs to be made as well, particularly as the equivalent behaviour can be done in userland*. btw It is possible, that the thing mostly likely to happen, and provide the most benefit for you, would be support in your IDE for autocompletion as implemented in userland. Which might take a lot less effort than implmenting the feature in PHP core... cheers Dan Ack * userland implementation: /** * @method sum(int $a, int $b): int; * @method sum(float $b, float $b): float; */ class Foo { public function __call($name, $arguments) { if ($name === 'sum' && is_int($arguments[0]) && is_int($arguments[1])) { return $this->sumInts($arguments[0], $arguments[1]); } if ($name === 'sum' && is_float($arguments[0]) && is_float($arguments[1])) { return $this->sumFloats($arguments[0], $arguments[1]); } throw new \InvalidArgumentException("Don't know how to call this"); } private function sumInts(int $a, int $b): int { // echo "sumInts was called.\n"; return $a + $b; } private function sumFloats(int $a, int $b): int { // echo "sumFloats was called.\n"; return $a + $b; } } $foo = new Foo(); $foo->sum(4, 4); $foo->sum(4.1, 4.1);