Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:105472 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 99847 invoked from network); 27 Apr 2019 22:35:45 -0000 Received: from unknown (HELO mail-io1-f43.google.com) (209.85.166.43) by pb1.pair.com with SMTP; 27 Apr 2019 22:35:45 -0000 Received: by mail-io1-f43.google.com with SMTP id u12so5747501iop.0 for ; Sat, 27 Apr 2019 12:37:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:from:date:message-id:subject:to; bh=TDM2q5ra8nJ6v1XbEJSInoVZp41J10RgSrk4Av/6kLo=; b=kFedRsjf2lW+Jgiu5mptxLQsiyoRLKc5F2KpzRt47EPCri/NNfLwrKF/OY+W0ZQtc3 1wcOUSz+SuPqZXP6lbp+ynDVAJGxP1d9EFTSTbQT+s5bc5oFLuCheK44pO4QGtADrJZE 5OE1ENVi5Gbp97SBhelGWPstgTGcLQmjN96Jnl+WWVsAdNoFotbyYNufRg5Oka25KWzJ pKmqF47h45zbuz/c5dPmII7n8S8juHvBwb10gV0p6KRjI5TBDr+E+uNiZH2OUCbdj6U5 qH+3CN2CH9H/WMyALSdQUvMkD1QwqHm102M5mHvzfdCEi8vbj4ntBdsSofsngDTbOFge YXXA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:from:date:message-id:subject:to; bh=TDM2q5ra8nJ6v1XbEJSInoVZp41J10RgSrk4Av/6kLo=; b=HwhyPJ1Df0f3bpkltiH54ICJbrlvT+Cjh0x2xkm7oXbonZ4c6dkc7x2lOumSqh2Plh CiQASBmxE0SNuu4fnmsJkDwdvsFCh5z2Jz0pflJ7hHouJh54PBHBV0QbD0DuTkZIU3w2 LERUDCcdFJIziA6IuLgcZ2WUvhmoFtupQEfuJ9CaoJn189k0yXTSBsKiaASuvVGQCaLz u+wIoDhIOlDcwVbr6E+3FM44i0Bkb972D4pavSgsJBRYug0tv6dsn2nn0sNU/HIwv9/e YgATHGm39OHTu3mC2pllX6NFuNFaHCbidEiYs/1+jZ2Lqth/1+ATAq5Q/UC4X5gkD9qp yOgg== X-Gm-Message-State: APjAAAXIhAUZkEB07vdoEfBVP/ch81FrC6LzeoL/qfGoehDU7h0Af7Fj fL1tK9vKWZowKhNvw3t4WMju8ABIRUwkujZbxsUQ/Q== X-Google-Smtp-Source: APXvYqzsHt17/sA7iLu4oXR1B9cTbYgxyT4ZmUI8lWHPi1mfSzn2tEcS1UMrfbDF8HK0vC4zHeTs+KQSUntDMcPM8DE= X-Received: by 2002:a5d:9489:: with SMTP id v9mr25823220ioj.129.1556393833064; Sat, 27 Apr 2019 12:37:13 -0700 (PDT) MIME-Version: 1.0 Date: Sat, 27 Apr 2019 16:37:01 -0300 Message-ID: To: PHP Internals Content-Type: multipart/alternative; boundary="0000000000008cee9d0587882dee" Subject: Symbol implementation From: david.proweb@gmail.com (David Rodrigues) --0000000000008cee9d0587882dee Content-Type: text/plain; charset="UTF-8" ES6 supports the Symbol type that is a replacement to hard-coded identifiers where it are not really need. I like to suggests a similar feature to PHP. #0: it should be a partial class that could not be instantiated (new symbol should be invalid, like an abstract class). In this point I should suggests two options: 1. It should works like a new keyword "symbol", but that could be extended like a class when need. (It will make sense on item #2). But could be referenced directly without need of the ::class. (eg. $a = symbol;). 2. Other option is use it as a new keyword "symbol" followed by a \Symbol reference class (like "instanceof Class" works). Eg. $a = symbol \Symbol; Anyway, maybe \Symbol could works as a default type, so only $a = symbol could be do the job. Or even consider symbol as a language constructor, allowing symbol(\Symbol), for instance. Ok, now to all make senses (I do expects), keep reading... (I will use otpion 2 as reference for now) #1: It could be used to fill constants, for instance, with "magic identifiers" that have no real value, needly. Eg.: public const USER_NOT_FOUND = symbol(), USER_INVALID = symbol(); Where USER_NOT_FOUND !=/!== USER_INVALID. #2: Another possibility is to extends it as an class, so it could be used as parameter typehint: (not available on ES6, I guess) class UserErrorSymbol extends \Symbol {} public const USER_NOT_FOUND = symbol(UserErrorSymbol), USER_INVALID = symbol(UserErrorSymbol); public function setError(UserErrorSymbol $error) {...} #3: it should have a fallback name (storing a scalar value), so in case you need to persist it (or serialize), so you could give a name: public const USER_NOT_FOUND = symbol(UserErrorSymbol as "UserNotFound"), USER_INVALID = symbol(UserErrorSymbol as "UserInvalid"), USER_INVALID_B = symbol(UserErrorSymbol as "UserInvalid"); symbol_alias(USER_INVALID) === 'UserInvalid' (string) USER_INVALID === 'UserInvalid' USER_INVALID !=/!== USER_INVALID_B #4: it could be used as a kind of anonymous array or object key: $arr[symbol()] = 1; $arr[symbol()] = 2; count($arr) === 2 $obj->{symbol()} = 1; $obj->{symbol()} = 2; count(get_object_vars($obj)) === 2 But symbol could not be accessed without a valid reference: echo $arr[symbol()]; // error: index symbol() is undefined or maybe symbol type could not be used directly to read an array value $symbol = symbol(); $arr[$symbol] = 1; echo $arr[$symbol]; // print 1 It is just a draft, but I do belive that it will be a good feature for PHP. --0000000000008cee9d0587882dee--