Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:94761 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 45264 invoked from network); 31 Jul 2016 09:00:57 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 31 Jul 2016 09:00:57 -0000 Authentication-Results: pb1.pair.com smtp.mail=mtkocak@gmail.com; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=mtkocak@gmail.com; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 74.125.82.48 as permitted sender) X-PHP-List-Original-Sender: mtkocak@gmail.com X-Host-Fingerprint: 74.125.82.48 mail-wm0-f48.google.com Received: from [74.125.82.48] ([74.125.82.48:36201] helo=mail-wm0-f48.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id B5/16-22978-54EBD975 for ; Sun, 31 Jul 2016 05:00:55 -0400 Received: by mail-wm0-f48.google.com with SMTP id q128so335859066wma.1 for ; Sun, 31 Jul 2016 02:00:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:from:date:message-id:subject:to; bh=gu/GlqCMaf0/stMivxBM7HtfSlsH92iC2/czkMrRGqY=; b=0hzjPPuZ4B7Nml9UAlx0PdZE4pGikQXWetWWZz8/MsjHcHEFxrTW2iaEVvtvFchhUK XGM/iTEhRp2hPwV4SFaSnSsLKrc0lcfJxhRxv8CKUAYUo0F+W9qUGzr+XouCCIkGxQhQ CiUnjOixtx97y+u41+nq3Dyb7j87WkGc23fLN4ad+OHyGn+2bJch5b9SlSCU9slEWTY7 dBjXmVyLmknX6vw1oMf74DBnjhJ5sBiB08ACoIIiCbhI7hus6y+I5YHknbDlM/BHezjM M+cWZsqu1WwIgDuF4QSMYJSDqCZw1DoUr9q2loAfeeQ21BtTlLIL6VR5LhKF9IGLyMgc VMvQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:from:date:message-id:subject:to; bh=gu/GlqCMaf0/stMivxBM7HtfSlsH92iC2/czkMrRGqY=; b=kGSqCxKm5tPq0zcHYkL4VKa7wWg8T8lUMS1t/U9Q4oiepgnSkpc/lzRIdK4U3oV3+w ibngeTgaJ9IYyPPszY8tWf0Yl6bIiyH4w1RK6nXZlh8sW54ybPKIY5CjnQSkZbcBp2qx 4PWQLOpXj6Cse1gQamtFY+MGJWxKFvKgNNiCfgucWMa3d9+PCN8p3dr461ZP/uxWOv0y QcEnU2Q7QfLkRLMeRUjSD9yXf89RYaIjAPBMU5cuzhtHYD1viGiyC2DGuo3xslT95ibq hM3khTCKJA27BP2mgEWz1nXLBl80yx06uzjUQN9SrmNCdygNRo5UG4ZHd1KHHn6RjUwg Qvdg== X-Gm-Message-State: AEkoouvr/rRn7eaozS07bcH9b3EUjFP0rGdhlD09hRqLIFyTt3mnDwd++H5rQrPJ0zeOOOgVXxVLEzu4gb3Zww== X-Received: by 10.28.198.6 with SMTP id w6mr8969036wmf.63.1469955649937; Sun, 31 Jul 2016 02:00:49 -0700 (PDT) MIME-Version: 1.0 Received: by 10.194.166.136 with HTTP; Sun, 31 Jul 2016 02:00:49 -0700 (PDT) Date: Sun, 31 Jul 2016 11:00:49 +0200 Message-ID: To: PHP Internals Content-Type: multipart/alternative; boundary=94eb2c19346659a75c0538eab8bf Subject: [RFC] Structured Object Notation From: mtkocak@gmail.com (=?UTF-8?Q?Midori_Ko=C3=A7ak?=) --94eb2c19346659a75c0538eab8bf Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable https://wiki.php.net/rfc/structured_object_notation Object Oriented Programming is a key feature to write structured programs in PHP. However, even though we have classes included in our program, the only way to create an object from a class, is to instantiate it in unstructured code. Consider this piece of code: get('/hello/{name}', function (Request $request, Response $response) = { $name =3D $request->getAttribute('name'); $response->getBody()->write("Hello, $name"); return $response; }); $app->run(); This causes loss of readability and benefits of oop in most of the programs. Most of the programs we write, end up to have some piece of spaghetti code somewhere. To prevent this, one should write a god object and run once but classes are not and should not be code blocks wrapped around =E2=80=9Cclass=E2=80=9D keyword. Here in this RFC, I propose a struc= tured way to instantiate objects in a PHP script in a structured way. Proposal According to most definitions an object is defined as: =E2=80=9CReal-world = objects share two characteristics: They all have state and behavior.=E2=80=9D Curre= ntly to interact with an object, one should instantiate the object and send messages manually within an unstructured code blocks. According to PHP manual a class is defined like this: foo(); A::foo(); $b =3D new B(); $b->bar(); B::bar(); ?> The part after class definitions is an unstructured program block where we lose readability, reusability, mantainability and all the other benefits of oop. Most of cli apps we use are showing what are avaliable commands and subcommands recursively: $ composer ... Avaliable Commands: about - ... archive - ... browse - ... $ composer browse -h Arguments: packages - ... Inspired by this, an object we create should also orient us where to go after each command. I will explain this later. Let's have one class called B, similar to above example. class B{ // No way to define order of methods public function __construct($world =3D "world"){ echo "Hello ". $world."\n"; echo "You can foo and you can bar\n"; } public function foo($x, $y){ echo "\nYou did foo: x is ".$x." and y is ".$y."\n"; echo "Now you can bar\n"; } public function bar($z, $t){ echo "\nYou did bar: z is ".$z." and t is ".$t."\n"; echo "You can bar again or end the program\n"; } public function __destruct(){ echo "\nGood bye\n"; } } // Here comes the spaghetti $app =3D new B("midori"); $app->foo(1,2); $app->bar(3,4); Instead of instantiating objects on the fly, here I propose a structured definition of an object using this syntax: object $app instanceof B{ $world =3D "midori"; // allowed methods comes after method definition // in the beginning state allowed methods are foo and bar public function __construct($world){ // if method body is implemented, // parent class method run automatically // object method runs after. // This block runs $app =3D new B($world); $this->foo(1,2); }(foo, bar); // The only allowed method is bar in this state public function foo(1,2)(bar); // if allowed methods are empty or not defined // object is destructed public function bar(3,4){ }; } An object can be instance of a class, if it's not defined, is instance of an anonymous class. --=20 Midori Ko=C3=A7ak Computer Scientist & Engineer http://www.mynameismidori.com =E2=80=9C*The most beautiful thing we can experience is the mysterious. It = is the source of all true art and science.*=E2=80=9D Albert Einstein --94eb2c19346659a75c0538eab8bf--