这是我的代码。我正在尝试访问bookName和bookAuthor。但变量是静态设置的。我不想将其更改为公开。但我想访问这些值。我该怎么做?
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class Book
{
private $bookName;
private $bookAuthor;
public function __construct($name, $author)
{
$this->bookName = $name;
$this->bookAuthor = $author;
}
public function getNameAndAuthor()
{
return $this->bookName . ' - ' . $this->bookAuthor;
}
}
class BookFactory
{
public static function create($name, $author)
{
return new Book($name, $author);
}
}
class FactoryController extends Controller
{
public function index()
{
$book1 = BookFactory::create('Laravel', 'Imrul');
$book2 = BookFactory::create('ReactJS', 'Hasan');
$book1->getNameAndAuthor();
$book2->getNameAndAuthor();
// dump($book1);
// dd($book1);
return view('home', compact(['book1', 'book2']));
}
}
home.blade.php
<h3>{{ $book1->bookName }}</h3>
<h3>{{ $book1->bookAuthor }}</h3>
翻阅古今
饮歌长啸