PHP5成员重载错误: Indirect modification of overloaded property has no effect

Posted in PHP on January 14th, 2010 by Adam
PHP5成员重载的时候
Class Foo{
private $arr;
public function __get($key) {
if(isset($this->arr[$key])){
return $this->arr[$key];
}
}public function __set($key, $val) {
if(isset($this->arr[$key])){
$this->arr[$key] = $val;
}
}
}
$f = new Foo;
$f->test = 1;
出现如下错误:
Notice: Indirect modification of overloaded property Foo::$arr has no effect
根据Google结果:
[27 Sep 2007 8:16pm UTC] brjann at gmail dot com
It seems that declaring the getter as “public function &__get(){…}” does the trick. However, it took some googling to find.
php magic __set and __get
this one works as expected. Actually php interpreter seems to do same thing. Trying to get value if exists and change it. Problem is that when it takes this value there is no place to store it and rise this error. Trying to modify property in not existing array. Solution is to make this array available. Returning values by reference seems to work.
解决办法是给__get()加上引用传递&__get,
public function &__get($key) {
return $this->arr[$key];
}
$f->array['test'] = 1;
试图帮助函数找到引用被绑定的不存在的变量arr。但是这里存在没有调用__set的问题。
我的办法是$arr转为公有化,放弃__set。
public $arr;
$f->arr['test'] = 1;
Tags:

Objects on PHP5, Javascript and AS3

Posted in 技术研究 on July 18th, 2008 by Adam
PHP5, Javascript and AS3都有面向对象(Object)一说,但它们之间也有着不小的差异。作为我比较喜欢的3种编程语言,我很想把它们放在一起,做一个横向的粗浅的比较。
AS3 (Action Script 3.0)
在AS3里每个对象都是类,这个类可以被看作是对象的模板或蓝图。类似于Java,融合了多种语言的特点。不支持嵌套类和私类。感觉太靠近Java,太过严谨的语法让制作Flash的时候失去了一些乐趣,多了一些沉闷。毕竟FLASH不只是AS。
最简单的AS3 Class例子
package mypackage
{
public class MyClass
{
public var textVariable:String = “some default value”;
public var numericVariable:Number = 17;
public var dateVariable:Date;
public function myMethod(param1:String, param2:Number):void
{
// do something with parameters
}
public function MyClass() // constructor
{
textVariable = “Hello there!”;
dateVariable = new Date(2001, 5, 11);
}
} class MySubClass extends MyClass
{
private var numericVariable2:Number = 1;
override public function myMethod(param1:String, param2:Number):Number
{
// do something with parameters
}
} }
Javascript
Javascript是一种基于原型(prototype)的语言,就像一个具有初始值得模板,任何对象都可以作为另一个对象的原型。灵活度较高,比较随意。目前最满意的就是它了。这里有一个我写的浮动层的例子,很喜欢这种编程方式:)。
Object.prototype.inObj = 1;
function A()
{
this.inA = 2;
}
A.prototype.inAProto = 3;
B.prototype = new A; // Hook up A into B’s prototype chain
B.prototype.constructor = B;
function B()
{
this.inB = 4;
}
B.prototype.inBProto = 5;
x = new B;
document.write(x.inObj + ‘, ‘ + x.inA + ‘, ‘ + x.inAProto + ‘, ‘ + x.inB + ‘, ‘ + x.inBProto);
source
PHP5
PHP5借鉴了Java2的对象模型,类型指示弱于AS3。PHP5向OOP靠近是应该的,也是必然的。
class SimpleClass
{
public $var = ‘a default value’;
public $public = ‘Public’;
protected $protected = ‘Protected’;
private $private = ‘Private’; public function displayVar() {
echo $this->var;
} function __construct() {
print “In constructor\n”;
$this->name = “MyDestructableClass”;
} function __destruct() {
print “Destroying ” . $this->name . “\n”;
}
}
$instance = new SimpleClass();
$assigned = $instance;
$reference =& $instance;
$instance->var = ‘$assigned will have this value’;
$instance = null; // $instance and $reference become null
Tags: , , ,

PHP5: Non-static method should not be called statically

Posted in PHP on June 25th, 2008 by Adam

今天发现PHP5调用静态方法的时候出现如下的错误:

Error message: Non-static method My_Class::mystatic() should not be called statically

根据网上搜索的结果:

These messages are generated in compile time, all the functions are
executed AFTER that, so error_reporting(0); does not have any effect and
this is expected behaviour. – 来源

这个错误只是一个E_STRICT错误(Runtime Notice),不会影响其后的程序执行。解决办法可以隐藏E_STRICT的报告。

另外:关于File_PDF找不到File/PDF/fonts/courierb.php的问题。
File_PDF最新源文件里面也没有这个courierb.php文件,先暂时复制courier.php为courierb.php。

Tags: ,
RSS Feed