1

ファイル " super.class.php"に次のクラスがあります

<?php    
class super {

        public function loadme() {
            $tilt = "I am tilted";
            $straight = "I am Straight";
            $round = "I am round";
            $sleep = "I am Sleeping";
        }

        public function fireme() {
            $hit = "I am hit";
            $bullet = "I got bullet";
            $gun = "Cover me! I am receiving Heavy Firing";
        }
}
?>

各変数を他のファイルに出力したい。注:クラスはから呼び出されますspl_autoload_register

印刷しようとしているとき:-

<?php
    $obj_super = new super();
    echo $obj_super->loadme()->tilt;
    echo $obj_super->fireme()->hit;
?>

エラーが発生しています:-Trying to get property of non-object in agent.php

更新:これで機能しました

以下で間違っていないことを願っています:-

<?php    
    class super {

        public $title = array();
        public $situation = array();

        public function loadme() {
            $this->title['tilt'] = "I am tilted";
            $this->title['straight'] = "I am Straight";
            $this->title['round'] = "I am round";
            $this->title['sleep'] = "I am Sleeping"; 
            return $this->title;
        }

        public function fireme() {
            $this->situation['hit'] = "I am hit";
            $this->situation['bullet'] = "I got bullet";
            $this->situation['gun'] = "Cover me! I am receiving Heavy Firing";
            return $this->situation;
        }
    }
    ?>

    <?php
        $obj_super = new super();
        $value = $obj_super->loadme();
        echo $value['tilt'];
    ?>

ありがとう

4

6 に答える 6

2

次のように、クラスに変数を追加する必要があります。

class super {
    public $tilt;
    public $straight;
}

そして、それらを設定します

public function loadme(){
    $this->tilt = "I am tilted";
    $this->straight = "I am straight";
}

あなたはそれらを得ることができます

$new = new super;
$new->loadme();
echo $new->tilt;
于 2013-09-21T04:03:33.510 に答える
2

OK、PHP で関数がどのように機能するかを誤解しているようです。関数で宣言した変数は関数に対してローカルです。つまり、関数のスコープ内でのみ値を持ちます。

第二に、表記法は、何も返さないので、の戻り$obj_super->loadme()->tilt値のプロパティの値を示します-あなたは本質的にこれを行っています。ログを確認すると、あらゆる種類のエラーが発生する可能性があります。tiltloadme()loadme()null->tilt

ここで何を達成しようとしていますか?

于 2013-09-21T04:04:17.300 に答える
1

呼び出している正確な方法で呼び出したい場合は、各関数からオブジェクト自体を返すことができます。以下を試してください

class super {
    public $tilt = '';
    public $straight = '';
    public $round = '';
    public $sleep = '';
    public $hit = '';
    public $bullet = '';
    public $gun = '';
    public function loadme() {
        $this -> tilt = "I am tilted";
        $this -> straight = "I am Straight";
        $this -> round = "I am round";
        $this -> sleep = "I am Sleeping";
        return $this;
    }

    public function fireme() {
        $this -> hit = "I am hit";
        $this -> bullet = "I got bullet";
        $this -> gun = "Cover me! I am receiving Heavy Firing";
        return $this;
    }

}

$obj_super = new super();
echo $obj_super -> loadme() -> tilt;
echo $obj_super -> fireme() -> hit;

@TheNytangel が示唆したように、以下もこのクラスで機能します。この動作を抑制したい場合は、関数をオブジェクトとして再設計する必要がある場合があります。

$obj_super = new super();
$obj_super -> loadme();
echo $obj_super -> tilt;
$obj_super -> fireme();
echo $obj_super  -> hit;
于 2013-09-21T04:13:51.073 に答える
0

OPのコードに従って、変更を加えます。インスタンス変数を削除しました。新しいフィドルはこちらhttp://phpfiddle.org/main/code/5qe-hn6

<?php    
class super {

    public function loadme() {
        $title=array();
        $title['tilt'] = "I am tilted";
        $title['straight'] = "I am Straight";
        $title['round'] = "I am round";
        $title['sleep'] = "I am Sleeping"; 
        return $title;
    }

    public function fireme() {
        $situation=array();
        $situation['hit'] = "I am hit";
        $situation['bullet'] = "I got bullet";
        $situation['gun'] = "Cover me! I am receiving Heavy Firing";
        return $situation;
    }
}
?>

<?php
    $obj_super = new super();
    $value = $obj_super->loadme();
    echo $value['tilt'];
?>
于 2013-09-21T05:03:25.183 に答える