Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:46090 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 21790 invoked from network); 18 Nov 2009 19:59:51 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 18 Nov 2009 19:59:51 -0000 Authentication-Results: pb1.pair.com header.from=stas@zend.com; sender-id=pass Authentication-Results: pb1.pair.com smtp.mail=stas@zend.com; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain zend.com designates 63.205.162.117 as permitted sender) X-PHP-List-Original-Sender: stas@zend.com X-Host-Fingerprint: 63.205.162.117 us-mr1.zend.com Linux 2.4/2.6 Received: from [63.205.162.117] ([63.205.162.117:42239] helo=us-mr1.zend.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id CB/04-16241-532540B4 for ; Wed, 18 Nov 2009 14:59:51 -0500 Received: from us-gw1.zend.com (us-ex1.zend.net [192.168.16.5]) by us-mr1.zend.com (Postfix) with ESMTP id 2B9C3E123E; Wed, 18 Nov 2009 11:57:25 -0800 (PST) Received: from [192.168.16.93] ([192.168.16.93]) by us-gw1.zend.com with Microsoft SMTPSVC(6.0.3790.3959); Wed, 18 Nov 2009 11:59:47 -0800 Message-ID: <4B045234.1010804@zend.com> Date: Wed, 18 Nov 2009 11:59:48 -0800 Organization: Zend Technologies User-Agent: Thunderbird 2.0.0.23 (Windows/20090812) MIME-Version: 1.0 To: Jingcheng Zhang CC: internals@lists.php.net References: In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-OriginalArrivalTime: 18 Nov 2009 19:59:47.0349 (UTC) FILETIME=[AE0EBC50:01CA6889] Subject: Re: [PHP-DEV] The inconsistencies/flaws of PHP5's object model From: stas@zend.com (Stanislav Malyshev) Hi! > I've just occured a syntax problem in the following script: > > class C { > public $n = 1; > } > $o = new C(); > $o->f = function () use ($o) { > echo $o->n; > }; > $o->f(); > ?> > > The result of this script is "Fatal Error: Call to undefined method C::f()". > I don't know this is the expected result. After trying more tests of Yes, this is the expected result. PHP is not Javascript, sorry :) Methods and properties are different things in PHP, so the syntax assumes you refer to method when you call $o->f(). You could use other syntaxes if you need to use a property as callable (see call_user_function, etc.). > 1. There is no way to add/remove instance-level members (both properties and > methods) to class dynamically, but a way to add them to instance itself, > which is a little buggy as above codes turns out; It is not "buggy", it is how the language is - class is a fixed template, object is a mutable instance of this template. > 3. There are __get(), __set(), __call() for instance-level members, and > __callStatic() for static methods, but lacks __getStatic() and __setStatic() > for static properties; That may be added if somebody provides a good patch. > 4. While using static class as object (general concept of "object", not > "instance" here), it's extremely complex to simulate "prototype object", as > static members simply do not duplicate themselves at all while inheriting, > therefore all of the child classes share a single static member of the > parent class; That's how static members work - they belong to the defining class. > 6. Static methods are allowed in interfaces, but not allowed in abstract > class, which breaks the rule of abstraction; You can have static functions in abstract classes. What you can not have is abstract static functions, because it makes little sense - static function belongs to the class, so if it's abstract - meaning needs to be redefined - then what exactly you are defining? You'll define static functions in child classes anyway and you'll be calling them explicitly by class name (i.e. ChildClass::foo, not BaseClass::foo). > 7. An interface which has only static methods cannot ensure static methods > in a class which implements it. I'm not sure what you mean here, could you provide a short code example that you think should be working and doesn't? -- Stanislav Malyshev, Zend Software Architect stas@zend.com http://www.zend.com/ (408)253-8829 MSN: stas@zend.com