UserFactory
<?php
namespace PHPBestPractices1OOP\Domain\User;
use PHPBestPractices1OOP\Domain\UsersTransactions\UsersTransactions;
use PHPBestPractices1OOP\Domain\Restaurant\RestaurantFactory;
use PHPBestPractices1OOP\Domain\Food\FoodFactory;
class UserFactory
{
/**
* @var UsersTransactions
*/
private $usersTransactions;
/**
* @var FoodFactory
*/
private $foodFactory;
/**
* @var RestaurantFactory
*/
private $restaurantFactory;
/**
* UserFactory constructor.
* @param UsersTransactions $usersTransactions
* @param FoodFactory $foodFactory
* @param RestaurantFactory $restaurantFactory
*/
public function __construct(
UsersTransactions $usersTransactions,
FoodFactory $foodFactory,
RestaurantFactory $restaurantFactory
) {
$this->usersTransactions = $usersTransactions;
$this->foodFactory = $foodFactory;
$this->restaurantFactory = $restaurantFactory;
}
/**
* create an instance of User class
*
* @param int $userId
* @return User
*/
public function createUser($userId)
{
$userObject = new User();
// get user data from the db and set to User object
$userRow = $this->usersTransactions->getUserById($userId);
$userObject->setName($userRow["userRow"]["name"]);
$userObject->setAge($userRow["userRow"]["age"]);
// create restaurant object and set user's favorite restaurant
$restaurantObject = $this->restaurantFactory->createResturant($userRow["userRow"]["favoriteRestaurantId"]);
$userObject->setFavoriteRestaurant($restaurantObject);
// create food object and set user's favorite food
$foodObject = $this->foodFactory->createFood($userRow["userRow"]["favoriteFoodId"]);
$userObject->setFavoriteFood($foodObject);
return $userObject;
}
/**
* create collection of User class instances for user's ids
*
* @param array $userIdsArray
* @return array
*/
public function createUsersCollection($userIdsArray)
{
// users collection
$usersCollection = array();
// loop through users array
foreach ($userIdsArray as $userId) {
$usersCollection[] = $this->createUser($userId);
}
return $usersCollection;
}
}
UserFactory
<?php
namespace PHPBestPractices1OOP\Domain\User;
use PHPBestPractices1OOP\Domain\UsersTransactions\UsersTransactions;
use PHPBestPractices1OOP\Domain\Restaurant\RestaurantFactory;
use PHPBestPractices1OOP\Domain\Food\FoodFactory;
class UserFactory
{
/**
* @var UsersTransactions
*/
private $usersTransactions;
/**
* @var FoodFactory
*/
private $foodFactory;
/**
* @var RestaurantFactory
*/
private $restaurantFactory;
/**
* UserFactory constructor.
* @param UsersTransactions $usersTransactions
* @param FoodFactory $foodFactory
* @param RestaurantFactory $restaurantFactory
*/
public function __construct(
UsersTransactions $usersTransactions,
FoodFactory $foodFactory,
RestaurantFactory $restaurantFactory
) {
$this->usersTransactions = $usersTransactions;
$this->foodFactory = $foodFactory;
$this->restaurantFactory = $restaurantFactory;
}
/**
* create an instance of User class
*
* @param int $userId
* @return User
*/
public function createUser($userId)
{
$userObject = new User();
// get user data from the db and set to User object
$userRow = $this->usersTransactions->getUserById($userId);
$userObject->setName($userRow["userRow"]["name"]);
$userObject->setAge($userRow["userRow"]["age"]);
// create restaurant object and set user's favorite restaurant
$restaurantObject = $this->restaurantFactory->createResturant($userRow["userRow"]["favoriteRestaurantId"]);
$userObject->setFavoriteRestaurant($restaurantObject);
// create food object and set user's favorite food
$foodObject = $this->foodFactory->createFood($userRow["userRow"]["favoriteFoodId"]);
$userObject->setFavoriteFood($foodObject);
return $userObject;
}
/**
* create collection of User class instances for user's ids
*
* @param array $userIdsArray
* @return array
*/
public function createUsersCollection($userIdsArray)
{
// users collection
$usersCollection = array();
// loop through users array
foreach ($userIdsArray as $userId) {
$usersCollection[] = $this->createUser($userId);
}
return $usersCollection;
}
}
- 145.6k
- 22
- 190
- 479
OOP, real-world User + related data class design Displaying a user profile based on a mock ORM
This is an OOP question and probably answerable even if you don’t have PHP knowledge. (A side note – we are learning PHP OOP and decided to create a github to hopefully help others learn this and other best practices a bit easier, https://github.com/nizamani/PHPBestPractices1-OOP if you are interested. PRs, issues etc are very welcome)
Overview
Overview We are learning PHP OOP and decided to create a simple app with 3 database tables, one a basic users table, the other two containing information about the users' favorite food and favorite restaurant.
A simple app with 3 database tables, oneTo help help others learn this and other best practices a basic users tablebit easier, the other two containing information about the users' favorite foodwe welcome PRs, issues, and favorite restaurantother contributions to our GitHub repository.
Goal/Output
Goal/Output
Our current goal (exact question will be listed below) is to create two pages:
Database
Database
In practice these are sql databases, note though if you happen to look at our github we've "faked" this db using php arrays"faked" this db using PHP arrays to make the project easier to run and try out
(https://github.com/nizamani/PHPBestPractices1-OOP/blob/master/src/fakedatabase/db.php )
User class
Our Current User class (we also have Restaurant and Food classes) Here is our User.php . We also have Restaurant and Food classes.
(https://github.com/nizamani/PHPBestPractices1-OOP/blob/master/src/classes/Domain/User/User.php )
Our Question
Our Question
Thanks for taking a look, we appreciate it.
OOP, real-world User + related data class design
This is an OOP question and probably answerable even if you don’t have PHP knowledge. (A side note – we are learning PHP OOP and decided to create a github to hopefully help others learn this and other best practices a bit easier, https://github.com/nizamani/PHPBestPractices1-OOP if you are interested. PRs, issues etc are very welcome)
Overview
A simple app with 3 database tables, one a basic users table, the other two containing information about the users' favorite food and favorite restaurant.
Goal/Output
Our current goal (exact question will be listed below) is to create two pages:
Database
In practice these are sql databases, note though if you happen to look at our github we've "faked" this db using php arrays to make the project easier to run and try out
(https://github.com/nizamani/PHPBestPractices1-OOP/blob/master/src/fakedatabase/db.php )
Our Current User class (we also have Restaurant and Food classes)
(https://github.com/nizamani/PHPBestPractices1-OOP/blob/master/src/classes/Domain/User/User.php )
Our Question
Thanks for taking a look, we appreciate it.
Displaying a user profile based on a mock ORM
Overview
We are learning PHP OOP and decided to create a simple app with 3 database tables, one a basic users table, the other two containing information about the users' favorite food and favorite restaurant.
To help help others learn this and other best practices a bit easier, we welcome PRs, issues, and other contributions to our GitHub repository.
Goal/Output
Our current goal is to create two pages:
Database
In practice these are sql databases, note though if you happen to look at our github we've "faked" this db using PHP arrays to make the project easier to run and try out
User class
Here is our User.php . We also have Restaurant and Food classes.
Our Question
Our current userUser
class works fine, but is slower than it could be for Page 1. Page 1 only requires the user's name, Jenn, which is data in the users
table. Yet we still have to get data from the restuarants
and foods
tables in order to create our user object.
We imagine that in the future half of our app's pages only need info from the users
table, while the other half of our app's pages need info from all 3 tables. Since we want our app to be fast and responsive, would it be a good idea to have our user
class only include things from the users
table, and then maybe create another class userDetailUserDetail
which includes the Food and Restaurant objects? On pages that we only need to display basic user table info, we use our userUser
class, and on pages where we need to display info about their favorite food and restaurant we use userDetailUserDetail
class? Or is there some other, better way to do this?
Our current user
class works fine, but is slower than it could be for Page 1. Page 1 only requires the user's name, Jenn, which is data in the users
table. Yet we still have to get data from the restuarants
and foods
tables in order to create our user object.
We imagine that in the future half of our app's pages only need info from the users
table, while the other half of our app's pages need info from all 3 tables. Since we want our app to be fast and responsive, would it be a good idea to have our user
class only include things from the users
table, and then maybe create another class userDetail
which includes the Food and Restaurant objects? On pages that we only need to display basic user table info, we use our user
class, and on pages where we need to display info about their favorite food and restaurant we use userDetail
class? Or is there some other, better way to do this?
Our current User
class works fine, but is slower than it could be for Page 1. Page 1 only requires the user's name, Jenn, which is data in the users
table. Yet we still have to get data from the restuarants
and foods
tables in order to create our user object.
We imagine that in the future half of our app's pages only need info from the users
table, while the other half of our app's pages need info from all 3 tables. Since we want our app to be fast and responsive, would it be a good idea to have our user
class only include things from the users
table, and then maybe create another class UserDetail
which includes the Food and Restaurant objects? On pages that we only need to display basic user table info, we use our User
class, and on pages where we need to display info about their favorite food and restaurant we use UserDetail
class? Or is there some other, better way to do this?