2016-03-23 8 views
-2

私のコントローラにドメインオブジェクトをインスタンス化しようとしていますが、このエラーは "Attempted to call function "getAge" from namespace "App\Bundle\Controller""です。 これは私の構造のフォルダです:名前空間 "App Bundle Controller"から "getAge"関数を呼び出そうとしました

enter image description here

これは私のCow.phpです:

<?php 

namespace Domain; 

class Cow { 
    private $age; 

    public function __constructor($ag) { 
     $this.$age = $age; 
    } 

    public function getAge() { 
     return $this.$age; 
    } 
} 

マイコントローラ:

<?php 

namespace App\Bundle\Controller; 

use Symfony\Bundle\FrameworkBundle\Controller\Controller; 
use Domain; 

class CowController extends Controller 
{ 
    public function insertAction() 
    { 
     $cow = new Domain\Cow(10); 
     $age = $cow.getAge(); 

     return $this->render('AppBundle:cow:insert.html.twig'); 
    } 
} 

私はすでに名前空間のすべての組み合わせを試してみました:

use Domain; 
$cow = new Domain\Cow(10) 

use \Domain 
$cow = new \Domain\Cow(10) 

use \Domain 
$cow = new Domain\Cow(10) 

どうすればこの作品を作れますか?

+1

あまりにも多くのエラーがあります。 PHPの正しいOOP構文については、[manual](http://php.net/manual/en/language.oop5.php)をチェックしてください。 –

答えて

1

は、次のように置き換えてみてください:

<?php 

namespace Domain; 

class Cow { 
    private $age; 

    public function __construct($age) { 
     $this->age = $age; 
    } 

    public function getAge() { 
     return $this->age; 
    } 
} 

コントローラー:

<?php 

namespace App\Bundle\Controller; 

use Symfony\Bundle\FrameworkBundle\Controller\Controller; 
use Domain\Cow; 

class CowController extends Controller 
{ 
    public function insertAction() 
    { 
     $cow = new Cow(10); 
     $age = $cow->getAge(); 

     return $this->render('AppBundle:cow:insert.html.twig'); 
    } 
} 

基本的には、PHPのメソッド/プロパティアクセサのシンボルは->代わりの.です。また、名前空間内でクラスを使用するには、クラス全体までの経路を指定する必要があります。\Domain

+0

ありがとうございます。なぜ私は知りませんが、getAgeは常に0を返しています。 – MuriloKunze

+0

コンストラクタ内にエコー "aaa"を設定しましたが、画面には表示されません。 – MuriloKunze

+0

ohh、私は__constructorを使用していましたが、__construct haha – MuriloKunze

1

まず、私はその点線の呼び出しに慣れていません。 しかし、あなたの問題について:

はこれを試してみてください:

use Domain\Cow; 

$cow = new Cow(10); 
関連する問題