Hi!

I've just occured a syntax problem in the following script:

<?php
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
s...@zend.com   http://www.zend.com/
(408)253-8829   MSN: s...@zend.com

--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to