0

私はこのコードを持っています:

class int64(){
    var $h; var $l;
    function int64(){
        $this->$h=$h;
        $this->$l=$l;
    }
}

function int64copy($dst,$src){
    $dst.$h = $src.$h;
    $dst.$l = $src.$l;
}

関数を呼び出している間int64copy、そのことわざCatchable Fatal Error: object of the class int64 could not be converted to string in line

何か案が?

4

3 に答える 3

2

オブジェクトにdoc表記を使用することはできません-オブジェクトを連結しようとしているため、 int64::__toString() を呼び出します-失敗します。

編集:より良い例:

class int64 {

    public $h; 
    public $l;

    function __construct($h, $l) {
        $this->h = $h;
        $this->l = $l;
    }


    public function __toString()
    {
        return sprintf('h: %s, l: %s', $this->h, $this->l);
    }

}

$a = new int64(1, 2);
$b = clone $a;

echo $a;
于 2012-10-22T11:05:44.627 に答える
0

プロパティにアクセスするための表記は$obj->prop. それは a の->後に a が続きません$。それはクラスの内外の両方で使用されました。

.文字列連結演算子です。

それに加えて、他のいくつかのマイナーな修正が得られるはずです。

class int64 {

    public $h,
           $l;

    public function int64(){
        $this->h = $h;
        $this->l = $l;
    }

}

function int64copy($dst, $src){
    $dst->h = $src->h;
    $dst->l = $src->l;
}

内部の$hおよび変数にはまだ問題があります。それらはどこから来るのでしょうか?$lint64::int64()

于 2012-10-22T11:13:31.320 に答える
0

必要なのはcloneだけです:完全に複製されたプロパティを持つオブジェクトのコピーを作成することは、必ずしも望ましい動作ではありません。

class int64 {
    public $h;
    public $l;

    function __construct() {
    }
}

$src = new int64();
$src->h = "h";
$src->l = "l";

$dst = clone $src ;
echo $dst->h , " " , $dst->l ;
于 2012-10-22T11:15:18.157 に答える