Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:103271 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 11857 invoked from network); 27 Sep 2018 21:38:23 -0000 Received: from unknown (HELO mail-it1-f175.google.com) (209.85.166.175) by pb1.pair.com with SMTP; 27 Sep 2018 21:38:23 -0000 Received: by mail-it1-f175.google.com with SMTP id m9-v6so8626096ita.2 for ; Thu, 27 Sep 2018 10:46:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=fcFCIrUISykPivXz76mM6wCRClpMdOuCKk6huS09wVw=; b=f6OS4kHzt0VuLkGWN/zAcNkB+X3TdPnyc1VPuBRO/VbNbophpM0V25h6IIhJAcHpn6 2jVSeHX8MShQ01/Gl3LYxi3bZt40Uxh/MiX8hWp/kQX5aurC/dOwjJQVagYvsBWutoA1 H1zI3Og2WGuNgJzHnNDnQE0sop2E+bPz3UtOEV0vm8cMhnRKlRuV3QEEA2u8Bij5l+uu j8li3rda1eMHxg/C3uEhQIzIrTawjY/c1ZUf/Dzuk6MkKn/4ykyIh6ATBTHkd6o3+RM2 klAsucA305R1JqkUzPqVZL0hDVG2wgpL47drPriFvidGvGuvzeWIi/Wz+4qe/RbvSKRm 66hQ== 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=fcFCIrUISykPivXz76mM6wCRClpMdOuCKk6huS09wVw=; b=gvKiMhr937shUcubPgFwijoooDpfbUockq856MuyyDO7PISdymCG7Z4xDKpSkv26EM qmX36yxxgUYiTpenILVppSb0esNtIuWpoQZHhqZsenDbh0XAeSKcHAQmdCN6eODBy85v 5TDFHbXorQClJ0H3RwEH2lnRhGG4g3C8Ncct65ikdJVoGwVqoSfFIstoZUUrzJOPuLGF TRH2oxuCbwIwfmxwy59XabqC29NUulS+hZ8xcF8X7JszSmTdseUTeTQ1laeJ7MmyWo96 ZjiWuEd/u9VLMe3UR2ByWFPofdBy6EQbQW7jo8IFVGxOnM3fN8ZvKw48o49EF6OXi8Sv mBaA== X-Gm-Message-State: ABuFfoh4wn2sPgcB1NZttE1Cm8rwnp3ACVEDQyttt6Af5p5hi8iP4hwL bRKXR3JGlFswzeyf0/uzGYcOieVW8QzLKLMgmpM7Vbr3 X-Google-Smtp-Source: ACcGV61NvvvY963TJl8E2X+PU3g6hSJtWHa5xKQH1Ey7pdUz/Xj+e+pA0B65AlSi3HzjkOwpAbVCcm0/Saye0VoRedc= X-Received: by 2002:a02:541:: with SMTP id e62-v6mr10928512jad.81.1538070408610; Thu, 27 Sep 2018 10:46:48 -0700 (PDT) MIME-Version: 1.0 References: <1537824239.902316.28693.59265@mail.rambler.ru> In-Reply-To: Date: Thu, 27 Sep 2018 18:46:37 +0100 Message-ID: To: Andrey O Gromov Cc: PHP internals Content-Type: multipart/alternative; boundary="0000000000005848ae0576ddecf7" Subject: Re: Re: [PHP-DEV] Re: Pre proposal for "Class extension functions" From: rowan.collins@gmail.com (Rowan Collins) --0000000000005848ae0576ddecf7 Content-Type: text/plain; charset="UTF-8" On Thu, 27 Sep 2018 at 16:15, Andrey O Gromov wrote: > > 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. > I think the execution order being referenced here is if two libraries both declare the same extension function. Since PHP has no separate build process, it's perfectly possible to do this: // Library A defines extension function in a.php function PDO->magic(){ return 'magic'; } // Library B defines extension function in b.php function PDO->magic(){ return 'more magic!'; } // Code uses function in c.php $pdo = new PDO(...); echo $pdo->magic(); Depending on which order the files are processed, this can do one of several things: - c.php loaded first -> error for undefined function (fair enough) - a.php loaded first, then c.php -> 'magic' - b.php loaded first, then c.php -> 'more magic!' - a.php, then b.php, then c.php -> Error when loading b.php? 'magic' because a.php defined first? 'more magic!' because b.php overwrote the definition? > Extension function style > > function PDO->takeOne($sql, $default){ > //see "classic way" > return $value; > } > > $oneValue = $db->takeOne($sql, $default); > This seems like a poor example, as this would work fine with inheritance: class Extended_PDO { function takeOne($sql, $default){ //see "classic way" return $value; } } The only difference is that code has to opt in to use the Extended_PDO class, but that's probably a good thing, because it makes it explicit when you can call the new method, and gives you namespacing, autoloading, typehinting, etc There may be cases where this kind of "monkey patching" makes sense, but I think a clearer use case is needed to justify the many downsides of building it into the language. Regards, -- Rowan Collins [IMSoP] --0000000000005848ae0576ddecf7--