array of objects in an object in php - php

I am fairly new to php. I have classes called quiz and question. And the class quiz can have one or many questions. I have done a java representation
class Question () {
private int id;
private String question;
private String answer;
}
and the quiz class should be as follows,
class Quiz() {
private int id;
private List<Question> questionList;
}
my question is regarding how to represent the above java representation in php. please be kind enough to point me in the right direction.

getter and setter:
class Question {
private $id ;
private $question;
private $answer;
public function __construct(){
}
public function setId($value){
$this->id = $value;
}
public function setQuestion($value){
$this->question = $value;
}
public function setAnswer($value){
$this->answer = $value;
}
public function getId(){
return $this->id;
}
public function getQuestion(){
return $this->question;
}
public function getAnswer(){
return $this->answer;
}
}
class Quiz{
private $id;
private $questionList = array();
public function __construct(){
}
public function setQuestionList($value){
$this->questionList[] = $value;
}
public function getQuestionList(){
return $this->questionList;
}
}
//
$quiz = new Quiz();
//
$question = new Question();
$question->setId(1);
$question->setQuestion('question?');
$question->setAnswer('answer');
//
$quiz->setQuestionList($question);
//
$question = new Question();
$question->setId(2);
$question->setQuestion('question2?');
$question->setAnswer('answer2');
//
$quiz->setQuestionList($question);
//
//Getting questions
foreach($quiz->getQuestionList() as $object){
echo $object->getId().' - '.$object->getQuestion().' - '.$object->getAnswer().'<br />';
}

Generics type don't exist in standard PHP.
One way to represent such structure in PHP is :
class Question
{
private $id;
private $question;
private $answer;
}
class Quiz
{
private $id;
/**
* #var array<Question>
*/
private $questionList = array();
}
The docblock (#var ...) is just here to help others (and IDEs) to understand the structure of $questionList.
To add some check on $questionList content, you can use typed setter/adder :
public function addQuestion(Question $question)
{
$this->questionList[] = $question;
}

Related

How do I implement an abstract method with different parameters?

I'm trying to implement an abstract class in order to make my code more readable.
This is my TvShow class:
final class TvShow {
private const DEFAULT_NUMBER_OF_SEASONS = 3;
private const DEFAULT_DELIVERED = false;
private const DEFAULT_TITLE = "";
private const DEFAULT_GENDER = "";
private const DEFAULT_CREATOR = "";
private function __construct(
private string $title = self::DEFAULT_TITLE,
private string $creator = self::DEFAULT_CREATOR,
private int $seasons = self::DEFAULT_NUMBER_OF_SEASONS,
private string $gender = self::DEFAULT_GENDER,
private bool $delivered = self::DEFAULT_DELIVERED
) {}
public static function createWithTitleAndCreator(string $title, string $creator):self {
return new self($title, $creator);
}
public static function create(string $tile, string $creator, int $seasons, string $gender):self {
return new self($tile, $creator, $seasons, $gender);
}
public function compareTo(object $object): bool {
if($this->seasons > $object->numberOfSeasons()) {
return true;
}
return false;
}
public function numberOfSeasons() {
return $this->seasons;
}
}
And this is my Videogame class:
final class Videogame {
private const DEFAULT_HOURS = 10;
private const DEFAULT_DELIVERED = false;
private const DEFAULT_TITLE = "";
private const DEFAULT_GENDER = "";
private const DEFAULT_COMPANY = "";
public function __construct(
private string $title = self::DEFAULT_TITLE,
private int $estimatedHours = self::DEFAULT_HOURS,
private string $gender = self::DEFAULT_GENDER,
private string $company = self::DEFAULT_COMPANY,
private bool $delivered = self::DEFAULT_DELIVERED
) {}
public static function createWithTitleAndHours(string $title, int $estimatedHours):self {
return new self($title, $estimatedHours);
}
public static function create(string $title, int $estimatedHours, string $gender, string $company):self {
return new self($title, $estimatedHours, $gender, $company);
}
public function compareTo(object $object): bool {
if($this->estimatedHours > $object->numberOfHours()) {
return true;
}
return false;
}
public function numberOfHours() {
return $this->estimatedHours;
}
}
By having the compareTo() method repeated, I have chosen to implement an abstract class:
abstract class Product {
abstract protected function compareTo(object $object);
}
But I need to change the type "object" depending the class to prevent errors. For example:
public function compareTo(TvShow $object): bool {
if($this->seasons > $object->numberOfSeasons()) {
return true;
}
return false;
}
This code return an error
The excercise require to implement the method in an abstract class. How can I do? Thanks.
What about something like this then, as #arkascha commented use an interface that both classes implement
interface ComparableInterface {}
final class TvShow implements ComparableInterface {
...
final class Videogame implements ComparableInterface {
...
public function compareTo(ComparableInterface $object): bool {
$class = get_class($object);
switch ($class) {
case TvShow::class:
return ($this->seasons > $object->numberOfSeasons());
case ...
}

Php, is it OK to use traits for DI?

consider this example:
class MyClass
{
public function doSomething()
{
$this->injected->getIt();
}
}
so far so simple (apart from injected is not injected). So, in full version:
class MyClass
{
/**
* #var Injected
*/
private $injected;
public function __constructor(Injected $injected)
{
$this->injected = $injected;
}
public function doSomething()
{
$this->injected->getIt();
}
}
but I find it enoromous. Why to pollute my class with tons of code of DI? Lets split this into two entities:
trait MyClassTrait
{
/**
* #var Injected
*/
private $injected;
public function __constructor(Injected $injected)
{
$this->injected = $injected;
}
}
class MyClass
{
use MyClassTrait;
public function doSomething()
{
$this->injected->getIt();
}
}
its much nicer although I never seen anybody using it like this. Is it a good approach?
For example like this:
<?php
class Factory
{
private $services = [];
public function __construct() {
$this->services[self::class] = $this;
}
public function getByType($type){
if(isset($services[$type])){
return $services[$type];
}
if(class_exists($type)){
$reflection = new ReflectionClass($type);
$constructor = $reflection->getConstructor();
$parameters = [];
if($constructor)
foreach($constructor->getParameters() as $parameter){
if($parameter->getClass()) {
$parameters[] = $this->getByType($parameter->getClass()->name);
} else if($parameter->isDefaultValueAvailable()){
$parameters[] = $parameter->getDefaultValue();
}
}
return $services[$type] = $reflection->newInstanceArgs($parameters);
} // else throw Exception...
}
}
abstract class DI
{
public function __construct(Factory $factory) {
$reflection = new ReflectionClass(get_class($this));
foreach($reflection->getProperties() as $property){
preg_match('/#var ([^ ]+) #inject/', $property->getDocComment(), $annotation);
if($annotation){
$className = $annotation[1];
if(class_exists($className)){
$property->setAccessible(true);
$property->setValue($this, $factory->getByType($className));
} // else throw Exception...
}
}
}
}
class Injected
{
public function getIt($string){
echo $string.'<br />';
}
}
class DIByConstructor
{
/** #var Injected */
private $byConstructor;
public function __construct(Injected $injected) {
$this->byConstructor = $injected;
}
public function doSomething()
{
echo 'Class: '.self::class.'<br />';
$this->byConstructor->getIt('By Constructor');
echo '<br />';
}
}
class DIByAnnotation extends DI
{
/** #var Injected #inject */
private $byAnnotation;
public function doSomething()
{
echo 'Class: '.self::class.'<br />';
$this->byAnnotation->getIt('By Annotation');
echo '<br />';
}
}
class DIBothMethods extends DI
{
/** #var Injected */
private $byConstructor;
/** #var Injected #inject */
private $byAnnotation;
public function __construct(Factory $factory, Injected $injected) {
parent::__construct($factory);
$this->byConstructor = $injected;
}
public function doSomething()
{
echo 'Class: '.self::class.'<br />';
$this->byConstructor->getIt('By Constructor');
$this->byAnnotation->getIt('By Annotation');
echo '<br />';
}
}
$factory = new Factory();
$DIByConstructor = $factory->getByType('DIByConstructor');
$DIByConstructor->doSomething();
$DIByAnnotation = $factory->getByType('DIByAnnotation');
$DIByAnnotation->doSomething();
$DIBothMethods = $factory->getByType('DIBothMethods');
$DIBothMethods->doSomething();
Note that with #Kazz approaching (DI by Annotations) you cannot reference an Interface, instead you are referencing a Class. So this is good for fast instantiating with almost zero verbose but at the end, you are loosing all the DI potential.

Symfony2 structural composite pattern with entities

I am trying to implement a simple menu composite pattern.
These are the following classes i came up with.
MenuItem:
namespace MYNAME\MYBUNDLE\Entity;
use MYNAME\MYBUNDLE\Menu\MenuComponent;
class MenuItem implements MenuComponent
{
private $id;
private $name;
private $path;
private $parent;
private $visible;
private $createdOn;
private $templating;
private $attr;
private $children;
private $website;
private $position = 1;
public function __construct($name = null, $path = null, $attr = array(), $visible = true)
{
$this->name = $name;
$this->path = $path;
$this->visible = $visible;
$this->attr = $attr;
$this->createdOn = new \DateTime;
}
public function prePersist()
{
$this->createdOn = new \DateTime;
}
public function build()
{
$data['menu_item'] = $this;
$data['options'] = $this->attr;
if($this->hasChildren())
return $this->templating->render('MYBUNDLE:Menu:menu_dropdown.html.twig', $data);
if($this->isChild())
return $this->parent->getTemplating()->render('MYBUNDLE:Menu:menu_item.html.twig', $data);
return $this->templating->render('MYBUNDLE:Menu:menu_item.html.twig', $data);
}
public function __toString()
{
return $this->name;
}
public function setTemplating($templating)
{
$this->templating = $templating;
}
/**
* #return bool
*/
public function isChild()
{
return $this->hasParent();
}
/**
* #return bool
*/
public function hasParent()
{
return isset($this->parent);
}
/**
* #return bool
*/
public function hasChildren()
{
return count($this->children) > 0;
}
}
If left out the getters and setters to make it a bit shorter here.
As you can see this is the entity and it contains a build() function, however this function uses the render method which in my opinion shouldn't be in an entity.
MenuController
<?php
namespace MYNAME\MYBUNDLE\Controller;
use MYNAME\MYBUNDLE\Menu\Menu;
use MYNAME\MYBUNDLE\Entity\MenuItem;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Response;
class MenuController extends Controller
{
public function generateAction()
{
$menu = new Menu($this->get('templating'));
// load menu items
$items = $this->getDoctrine()->getRepository('MYBUNDLE:MenuItem')->findOrdered();
foreach($items as $item)
{
if(!$item->hasParent())
$menu->add($item);
}
return new Response($menu->build());
}
}
The MenuController gets called to render the menu:
{{ render(controller('MYBUNDLE:Menu:generate')) }}
I would also like this to be different since it doesn't look right. Perhaps it's better to create a twig function to render the menu?
MenuComponent:
namespace MYNAME\MYBUNDLE\Menu;
interface MenuComponent {
public function build();
}
Menu:
namespace MYNAME\MYBUNDLE\Menu;
class Menu implements MenuComponent
{
private $children;
private $templating;
public function __construct($templating)
{
$this->templating = $templating;
}
public function add(MenuComponent $component)
{
$component->setTemplating($this->templating);
$this->children[] = $component;
}
public function build()
{
return $this->templating->render('MYBUNDLE:Menu:menu.html.twig', array("menu_items" => $this->children));
}
}
Menu Contains the MenuComponents and will render the menu first, in each MenuItem it's build() method is called.
I think it's better to remove the rendering logic from my MenuItem entity and place this somewhere else, however i can't figure out on how to do this properly within this design pattern.
Any help or suggestion is appreciated.

Array Object In Php

i want to store Student Object to array. and i try to do with below code. but it always show array count as 0
class Student
{
$StudID = 0;
$Name = null;
}
class Students
{
static private $StudentData = array();
static public function AddNewStudent($id,$name)
{
echo("AuctionID :".$AuctionID."<br/>");
try{
$objstd = new Student();
$objstd->StuID = $id;
$objstd->Name = &name;
array_push($StudentData, $objstd);
}
catch (Exception $e)
{
echo("Error".$e->getMessage());
}
}
static public function TotalStudent()
{
return count($StudentData);
}
}
Students::AddNewStudent(1,"name");
Students::AddNewStudent(2,"name2");
Students::AddNewStudent(3,"name3");
echo('Total auction running : '.Students::TotalStudent().'<br/>');
when i try to show array count it shows 0. i want to store all student data in static list
or then after when ever i want to see the list i get the list from static class only...
Because you're creating a new array instead of referencing the one you declared. Use the self keyword to reference your static object property:
class Students
{
static private $StudentData = array();
static public function AddNewStudent($id,$name)
{
echo("AuctionID :".$AuctionID."<br/>");
try{
$objstd = new Student();
$objstd->StuID = $id;
$objstd->Name = &name;
array_push(self::$StudentData, $objstd);
}
catch (Exception $e)
{
echo("Error".$e->getMessage());
}
}
static public function TotalStudent()
{
return count(self::$StudentData);
}
}
In php you have to prefix static variables with self::, like this:
array_push(self::$StudentData, $objstd);
// and in count:
return count(self::$StudentData);
Why that complicated? Your Student class should take care of it's own, same for Students. Example:
$students = new Students();
$students[] = new Student(1, "name");
$students[] = new Student(2, "name2");
$students[] = new Student(3, "name3");
printf('Total auction running : %d.', count($students));
Example output:
Total auction running : 3.
The classes:
class Student
{
/**
* #var int
*/
private $id;
/**
* #var string
*/
private $name;
/**
* #param int $id
* #param string $name
*/
public function __construct($id, $name) {
$this->id = $id;
$this->name = $name;
}
/**
* #return string
*/
public function getName()
{
return $this->name;
}
/**
* #return int
*/
public function getId()
{
return $this->id;
}
}
class Students extends ArrayObject
{
public function __construct()
{
parent::__construct(array());
}
public function offsetSet($index, $newval) {
if (!($newval instanceof Student)) {
throw new InvalidArgumentException('You can only add values of type Student.');
}
parent::offsetSet($index, $newval);
}
}

PHP RecursiveIterator traversing

I have a structure representing a form and I want to iterate it using RecursiveIterator.
The problem is this only returns the top-level questions. What am I doing wrong?
Whole form:
class Form implements RecursiveIterator{
private $id;
private $caption;
private $other_text;
private $questions = array();
private $current;
private function __construct(DibiRow $row){
$this->id = $row->id;
$this->caption = $row->caption;
$this->other_text = $row->other_text;
$this->loadQuestions();
}
private function loadQuestions(){
$questions = dibi::query('SELECT * FROM cyp_questions WHERE form_id = %i AND parent_id IS NULL', $this->id);
while($question = $questions->fetch()) $this->questions[] = new Question($question->question_id, $question->type, $question->caption, $question->other_text, $question->triggers_unique == 1);
}
/**
* #throws InvalidArgumentException
* #param $id
* #return Form
*/
public static function loadById($id){
$form = dibi::query('SELECT * FROM cyp_forms WHERE id = %i', $id)->fetch();
if($form === false) throw new InvalidArgumentException('Form with id '.$id.' was not found.');
return new Form($form);
}
/**
* #throws FormFieldException
* #return bool
*/
public function validate($postfields){
}
public function getQuestions(){
return $this->questions;
}
public function getChildren(){
return $this->questions[$this->current];
}
public function hasChildren(){
return count($this->questions) > 0;
}
public function current(){
return $this->questions[$this->current];
}
public function key(){
return $this->current;
}
public function next(){
$this->current++;
}
public function rewind(){
$this->current = 0;
}
public function valid(){
return isset($this->questions[$this->current]);
}
}
Question:
class Question implements RecursiveIterator{
private $id;
private $type;
private $answers = array();
private $subquestions = array();
private $other_text;
private $triggers_unique;
private $caption;
private $current = 0;
public function __construct($id, $type, $caption, $other_text = null, $triggers_unique = false){
$this->id = $id;
$this->type = $type;
$this->caption = $caption;
$this->other_text = $other_text;
$this->triggers_unique = $triggers_unique;
$this->setSubQuestions();
}
private function setSubQuestions(){
$questions = dibi::query('SELECT * FROM cyp_questions WHERE parent_id = %i', $this->id);
while($question = $questions->fetch()) $this->subquestions[] = new Question($question->question_id, $question->type, $question->caption, $question->other_text, $question->triggers_unique == 1);
}
public function getOtherText(){
return $this->other_text;
}
public function getCaption(){
return $this->caption;
}
public function addAnswer($answer){
$this->answers[] = $answer;
}
public function getChildren(){
return $this->subquestions[$this->current];
}
public function hasChildren(){
return count($this->subquestions) > 0;
}
public function current(){
return $this->subquestions[$this->current];
}
public function key(){
return $this->id;
}
public function next(){
++$this->current;
}
public function rewind(){
$this->current = 0;
}
public function valid(){
return isset($this->subquestions[$this->current]);
}
public function getAnswers(){
return $this->answers;
}
}
Iteration:
$form = Form::loadById(1);
foreach($form as $question){
echo $question->getCaption().'<br />';
}
To iterate over a RecursiveIterator, you have to wrap it into a RecursiveIteratorIterator.
See some examples at
Introduction to Spl
SplWiki
The default iteration mode is only to list leaves. If you also want the containing nodes to appear in the iteration, pass RecursiveIteratorIterator::SELF_FIRST as the second argument to the constructor of the RecursiveIteratorIterator
Well, as you can see here
public RecursiveIterator RecursiveIterator::getChildren ( void )
Returns an iterator for the current iterator entry.
the method should return an object implementing the iterator. Your method return a simple array.
My guess would be to return something like:
public function getChildren(){
return new Question($this->subquestions);
}
This is because you're using a RECURSIVE iterator so it's expected to have each node of the tree of the same type (an iterator)

Categories