0

PHPスクリプトから、あるクラスから別のクラスを介して変数にアクセスしようとしています:

私の最初のクラスは次のとおりです。

クラスデータ{

  private $length;
  private $height;      

   public function setLength($length){
    $this->length = $length;
   }

   public function getLength(){
    return $this->length;
   }

  public function setHeight($height){
    $this->height = $height;
   }

   public function getHeight(){
    return $this->height;
   }

}

私は別のクラスを持っています:

class proccess extends data{

   public function getOrientation(){
      if($this->getLength() > $this->getHeight()) {
       $orientation = 'landscape';
      } else {
      $orientation = 'portrait';
   }

   }

}

クラス プロセスから $this->getLenght() または $this-getHeight() にアクセスしようとすると、値が空になります。次のように、phpスクリプトを使用して値を設定しています。

<?php


  require_once('functions/data.php');
  require_once('functions/process.php');

  $data=new data();
  $process = new process();

  $data->setLength(25);
  $data->setHeight(30);
  $orientation = $process->getOrientation();

関数 getOrientation が幅と長さの値を取得できない理由と、これを修正する方法についてのアイデアはありますか?

4

2 に答える 2

3

である別のオブジェクトの値を設定しています$data。に設定する必要があり$processます。

  $process = new process();

  $process->setLength(25);
  $process->setHeight(30);
  $orientation = $process->getOrientation();
于 2013-10-28T21:02:11.630 に答える
-1

変数はそうであってはなりprotectedませんprivate- これらを見てください:

http://php.net/manual/en/language.oop5.visibility.php 公開、非公開、保護の違いは何ですか?

そして、MahanGM が指摘したように、互いにまったく関係のない 2 つの異なるインスタンスのオブジェクトを使用しています。$process->setLengthand$process->setHeightまたはのいずれかを実行する必要があります$data->getOrientation

于 2013-10-28T21:02:11.363 に答える