If you don't plan to use getters and setters because you're just sending data directly to the widget for rendering and handling, and you expect the $quantity property to be a simple public property (perhaps for display rather than manipulation), that's perfectly fine!
You can maintain $quantity as a public property without the additional complexity of getters and setters if your design is straightforward and you don't require any special validation or logic beyond that.
Implementing Quantity as a Public Property
Here’s how you can keep $quantity as a public property in your Artwork class without getters and setters, while still ensuring that it holds the expected integer type:
namespace app\widgets;
use yii\base\Widget;
use yii\helpers\Html;
class Artwork extends Widget
{
public $heading = 'More Info';
public $title = 'Artwork Title';
public $titleUrl = null;
public $artist = 'Artist Name';
public $dimensions = '50cm x 70cm';
public $medium = 'Oil on canvas';
public $year = '1918';
public $description = 'This is a detailed description of the artwork.';
public $body = null;
public $quantity = 2; // Keep as a public property
public $price = '500';
public $purchaseAction = 'cart/purchase-now';
public $inquiryAction = 'modal/inquiry';
public function run()
{
$id = $this->getId();
$this->registerJs($id);
$this->registerCss();
return $this->renderAccordion($id);
}
protected function renderAccordion($id)
{
$headerId = $id . '-header';
$collapseId = $id . '-collapse';
return Html::tag('div',
Html::tag('h2', PHP_EOL . Html::button(Html::encode($this->heading), [
'class' => 'accordion-button collapsed',
'data-bs-toggle' => 'collapse',
'data-bs-target' => "#$collapseId",
'aria-expanded' => 'false',
'aria-controls' => $collapseId,
]), [
'id' => $headerId,
'class' => 'accordion-header'
]) . PHP_EOL .
Html::tag('div', PHP_EOL . Html::tag('div', $this->renderAccordionContent(), [
'class' => 'accordion-body'
]), [
'id' => $collapseId,
'class' => 'accordion-collapse collapse',
'aria-labelledby' => $headerId,
'data-bs-parent' => "#$id",
]), [
'id' => $id,
'class' => 'accordion accordion-flush'
]);
}
}
Usage Example
Your widget usage can remain the same. You interface with it by passing the quantities directly as public properties.
<?= Artwork::widget([
'heading' => 'Learn More',
'title' => $model->title,
'artist' => 'Leonardo da Vinci',
'dimensions' => '77cm x 53cm',
'medium' => 'Oil on poplar',
'year' => '1503',
'description' => 'A timeless piece of art illustrating the depth and genius of Leonardo.',
'price' => '65000',
'quantity' => 5, // Directly pass the quantity
]); ?>
Summary
In this case, if you're simply assigning values and there is no need for validation or input handling, you can keep the $quantity as a public property. This will work perfectly for a straightforward display scenario where you pass in model data without needing to handle proper access or encapsulation.
Conclusion
Encapsulation, using getters and setters, is a good practice, particularly for properties requiring validation or complex logic. However, if your application is simple and you are directly passing the data to render the content without conditional logic on those properties, then maintaining them as public properties is entirely acceptable. Choose the approach that aligns with your requirements and coding style!