Should I create an object or work with an array? - php

I am fairly new to OOP PHP and as I am building my first project, I am coming across some difficult dilemmas.
I'm trying to build a forum. Yes, I know there are many free ones out there, I just want to have one that I can build according to my own needs :). Plus, it's fun to code.
I have build a base model and controller and template, for the forum, thread and post.
The base model has standard database functions, like find all, find by id etc. The individual models extend the model class and have only specific functions to the class.
I have set it up like this:
class Post_Model extends Model {
static protected $_table_name = 'post';
static protected $_db_fields = array('id', 'thread_id', 'parent_id', 'username', 'user_id',
'title', 'date_line', 'pagetext', 'show_signature', 'ip_address', 'icon_id', 'visible');
/**
* #var db fields
*/
public $id;
public $thread_id;
public $parent_id;
public $username;
public $user_id;
public $title;
public $date_line;
public $pagetext;
public $show_signature;
public $ip_address;
public $icon_id;
public $visible;
}
/*
* joined fields with user object. needed to display posts in all details
*/
static public function get_posts($id, $limit, $offset){
$result_set = static::find_by_sql("
SELECT *
FROM post
LEFT JOIN user ON (post.user_id = user.id) where post.thread_id=".(int)$id."
ORDER BY date_line DESC
LIMIT {$offset} , {$limit}");
return !empty($result_set) ? $result_set : false;
}
And besides the decisions that were tough, that I already had to make about certain functions and variables being static or not. I have this dilemma:
The basic CRUD works from the base model. That is fine, I can create, update, delete and read from my database through calling static methods from the (base) model, which instantiates objects for these forum, thread and post models. So far so good.
Part of the Model Class:
<?php
/**
* Find rows from database based on sql statement
* #param string $sql
* #retun array $result_set
*/
static public function find_by_sql($sql = '') {
$db = Database::getInstance();
$mysqli = $db->getConnection();
$result = $mysqli->query($sql);
$object_array = array();
while ($record = $result->fetch_array()) {
$object_array[] = static::instantiate($record);
}
return $object_array;
}
private static function instantiate($record) {
$object = new static;
foreach ($record as $attribute => $value) {
if ($object->has_attritube($attribute)) {
$object->$attribute = $value;
}
}
return $object;
}
}
?>
I transfer these objects for displaying in my templates. When reading from one table, that works just fine.
<?php
$postcount = $pagination->offset + 1;
if (!empty($posts)) {
foreach ($posts as $post) {
?>
<div class="post">
<div class="posttime"><?php echo strftime("%A %e %B %G %H:%M", $post->date_line); ?></div>
<div class="postnumber">#<?php echo $postcount; ?></div>
<div class="postuser">
<div class="username"><?php echo $post->username; ?></div>
<div class="avatar">avatar</div>
</div>
<div class="postcontents">
<div class="posttext"><?php echo $post->pagetext; ?></div>
</div>
</div>
<?php
$postcount++;
}
?>
<div class="pagination"><?php echo $page_split; ?></div>
However, for the display of all posts in the threads, I need info from the user table, e.g. there number of their posts, rank, avatar etc.
Okay, so here's the thing:
SELECT * FROM post
LEFT JOIN user ON (post.user_id = user.id)
where thread_id={$id}
ORDER BY date_line DESC
LIMIT {$offset} , {limit}
I now have more fields then variables that I have setup in my Post model. So they are not transferred for displaying.
Should I use an array for displaying these extra fields instead of object? I would rather still use the object approach for displaying, as I am now set up for that.
Or should I maybe add these user fields as variables to my Post model? I am thinking that is not the most tidy way to do it, however it would work.
Or should I go through a foreach loop to get a separate instance of a (yet to create) user class? I am thinking it would drain much more memory then the Left Join that simply gets me the data that I need.
I would like some hints on how you would solve this.

One thing that might break out the dilemma; in PHP an object can also act as an array (or even a function!) but a normal plain array cannot have properties or any other OOP feature.

You won't be able to solve this with your schema, because you are mixing properties from different entities in your Post entity [i.e. username].
The idea is that you just have to make your code aware of the relationship between entities, like:
class Post extends Entity
{
protected $id;
protected $body;
protected $date;
// this will be an instance of a Thread entity, let's say
protected $thread;
// this will be an instance of the User entity
protected $user;
...
}
This way you will be able to do:
$somePost -> getUser() -> getAvatar();
But how do you inject the correct user and thread to each post? That's what Object Relational Mappers are for: in you case, all the SQL stuff would be handled by the ORM, and you'll be just dealing with you object graph, with no worries about building queries on your own; you just have to define such relationships.
A popular PHP ORM is Doctrine: here is an example about how it handles relationships declarations.
Of course this approach has downsides, but it's up to you investigating and finding out the best tradeoff between flexibility and dependencies.

Related

Doctrine fetch associated array for multiple objects

I've got two classes, Post and Comment, as components I'm using to build a news feed. Post has a one-to-many association with Comment.
When building my news feed, I query the posts table and typically return a chunk of 20 results. With these 20 results, I'd also like to load the comments associated with it. Logic dictates that I can simply use fetch="EAGER" on the #OneToMany annotation, but in this use case it doesn't scale well as searching through a comments table with 1,000's of entries repetitively for each post is not performant.
Ideally, I'd like to separately preload them in a single query - which is what I've done.
class Post {
// ...
/**
* #var Comment[]
*
* #ORM\OneToMany(targetEntity="App\Entity\PostComment", mappedBy="post", cascade={"persist", "remove"}, orphanRemoval=true)
*/
protected $comments;
public function getComments() {
return $this->comments;
}
public function setComments(array $comments) {
$this->comments = $comments;
}
}
class NewsFeedService {
private function preloadComments(array $posts) {
$postIds = array_column($posts, 'id'); // Get the post ID's
$comments = $this->manager->getRepository(Comment::class)->findCommentByPostIds($postIds);
// Group the comments by post
$groupedComments = [];
foreach($comments as $comment) {
$groupedComments[$comment->getPost()->getId()][] = $comment;
}
// Populate each $post object with it's comments
foreach($groupedComments as $postId => $postComments) {
$post = $posts[$postId];
$post->setComments($postComments);
}
}
}
class CommentRepository extends EntityRepository {
public function findCommentsByPostIds(array $postIds) {
return $this->createQueryBuilder('c')
->where('c.post IN(:ids)')
->setParameter('ids', $postIds)
->getQuery()
->getResult();
}
}
Essentially, when calling preloadComments() I run a single query on the DB and then force the results into each Post. Compared to EAGER fetching this saves me on average 35-40% with my current dataset (60,000 comments). I mean it works, but..
My question is if there is a better, perhaps even doctrine native way of doing this. There are also some things that I haven't tested such as if calling $post->setComments() and adding externally fetched data will cause issues if I just so happen to update the Post object and flush the changes. I feel like going about the way I'm doing isn't optimal and may cause some small headaches over the performance I'm saving.

Object oriented use of properties/methods in real webapplications?

I just moved from my "everything-in-functions" coding style to OOP in PHP. I know, there are a lot of sources in order to code properly, but my main problem is, after reading so many tutorials about coding a blog/cms/any web app in OOP it's quite not that understandable to me to code it the right way.
I was told the concept of OOP is basically close to the real world.
So properties are just properties of any object and methods are like what the object should do. But I can't really understand how to use these properties and methods in a web app. In the real world, it's easier than in a web application for me, for instance:
Let's say we want to create a human named "Paul" and he should drive a car.
class human
{
public $gender;
public $name;
function __construct($inGender=null, $inName=null) {
$this->gender = $inGender;
$this->name = $inName;
}
public function driveCar() {
//Let the car be driven.
//...
}
}
$paul = new human('male','Paul');
$paul->driveCar();
Alright, but when it comes to an object in a web application, let's say I want to code a blog system specifically let's focus the posts. Properties could be id, name, content, author of a post, BUT what does a post actually do? It can't really perform an action so I would code it like this:
class post
{
public $id;
public $title;
public $content;
public $tags;
//Overloading the Variables
function __construct($inId=null, $inTitle=null, $inContent=null, $inTags=null) {
if(!empty($inId) || $inId == 0) {
$this->id = $inId;
}
if(!empty($inTitle)) {
$this->title = $inTitle;
}
if(!empty($inContent)) {
$this->content = $inContent;
}
if(!empty($inTags)) {
$this->tags = explode(' ', $inTags);
}
}
}
//In order to pass multiple posts to an constructor I would use a foreach loop
$posts[] = new post(0,'Hello World', 'This is a test content','test blog oop');
$posts[] = new post(1,'Hella World!', 'This is made my myself','test test test');
//Usage in a template
<?php foreach($posts as $post): ?>
<h1><?= $post->title;?></h1>
<p><?= $post->content;?></p>
<?php endforeach; ?>
The problem here is the method does not do anything, what is not supposed to do I guess. And another problem would be, when I would store data into an array, I can't use a single array with data of posts as several parameters for the constructor. Even if this was a single post, how to accomplish the passing of data as an array properly in order to use the array as the parameters? Maybe I just coded it not the best way, but what would be a better way? I heard of setter and getter and magic methods. What would here best practise?
Alright, but another question. How would I program the create/delete posts functionality, new classes or new methods? I can't really imagine anything of this in the real world concept.
This all is not that clear for me since it's nowhere explained on any tutorial.
Help would be amazing.
regards
answering all your questions needs a book as understanding the oop concept completely needs much reading and practicing.
But to answer your post class I give you an easy to use example where you can create a class which will help you create, read, delete and update posts.
This class can be used in all your projects as it's a class and classes are simply there to make our life easier.
So here we go:
first we create the class:
class posts
{
//we just need a post title and a post content for the new post.
//So we create two properties for post_title and post_content
private $post_title; //the variables are private so we we restrict access the them from outside the class.
private $post_content;
private $connection;
//you really don't need here the _constructor function, but it's your choice.
//For example you can do all your database connection stuff in the constructor method to make it easy for users.
//So if someone creates a posts object it is obvious that he's going to make some interactions with the database so you open connections in your constructor.
//next we create the methods for our posts class
public function __constructor()
{
//here you created the connection to your database. You can retrieve
//the parameters to the mysqli_connect from a eg. config.php file to make your class more dynamic.
$this->connection = mysqli_connect('hostname', 'username', 'password', 'databasename');
}
public function create_post($title, $content)
{
$this->post_title=$title;
$this->post_content=$content;
$query="INSERT INTO posttable(id,title,content) VALUES('$this->post_title','$this->post_content')";
$result=$mysqli_query($this->connection,$query);
}
public function update_post($post_id, $post_title, $post_content)
{
}
public function delete_post($post_id)
{
}
public function view_posts($query, $fields)
{
$result=mysqli_query($this->connection, $query);
$finalData=array();
while ($row=mysqli_fetch_assoc($data)) {
foreach ($fields as $field) {
array_push($finalData, $row[$field]);
}
}
return $finalData;
}
}
//now we're going to use the class.
$post=new posts(); //a new posts object is created and the __construct method opened connection to db.
$post->create_post('some title', 'some content');//inserted new post into database.
$post->update_post($id, $content, $title);//you've just updated a post
$post->delete_post($id);//you've just deleted a post.
//getting posts
$query="SELECT * FROM posts";
$fields=array('id','title','content');
$all_posts=$post->view_posts($query, $fields);
//now you have $all_posts which is filled with an array of all posts from the posts table.
//now you can loop through it and show it on your page.
for ($i=0; $i<=count($all_posts); $i++) {
echo '<h1>'.$mydata[$i].'</h1>';
$i++;
echo '<h2>'.$mydata[$i].'</h2>';
$i++;
echo '<h3>'.$mydata[$i].'</h3>';
}
I didn't implement the update and delete method code but I think it's easy for you to write it by yourself.All my goal was to show you how you can use oop in your web applications. I personally have nearly 50 classes written by myself and do all the major jobs for nearly all possible web application tasks. It's up to you how you create your classes. Just for your knowledge, oop programming needs training and experience. You can do a task in multiple forms and with different solutions. You can find dozens of books which teach you oop design who's concept and goal is to teach you how to design a class to the best way.
I hope I was helpful

Models in mvc (best practices, PHP)

I know there are plenty of articles and questions about MVC and best practices, but I can't find a simple example like this:
Lets say I have to develop a web application in PHP, I want to do it following the MVC pattern (without framework).
The aplication should have a simple CRUD of books.
From the controller I want to get all the books in my store (that are persisted in a database).
How the model should be?
Something like this:
class Book {
private $title;
private $author;
public function __construct($title, $author)
{
$this->title = $title;
$this->author = $author;
}
public function getTitle()
{
return $this->title;
}
public function setTitle($title)
{
$this->title = $title;
return this;
}
.
.
.
class BooksService{
public getBooks(){
//get data from database and return it
//by the way, what I return here, should by an array of Books objects?
}
public getOneBook($title){
//get data from database and store it into $the_title, $the_autor
$the_book = new Book($the_title, $the_autor);
return $the_book;
}
.
.
.
So I call it(from the controller) like that:
$book_service = new BooksService();
$all_books = $book_service->getBooks();
$one_book = $book_service->getOneBook('title');
Or maybe should be better have everything in the Books class, something like this:
class Book
{
private $title;
private $author;
//I set default arguments in order to create an 'empty book'...
public function __construct($title = null, $author = null)
{
$this->title = $title;
$this->author = $author;
}
public function getTitle()
{
return $this->title;
}
public function setTitle($title)
{
$this->title = $title;
return this;
}
public getBooks(){
//get data from database and return it
//and hare, what I should return? an Array?
}
public getOneBook($title){
//get data from database and store it into $the_title, $the_autor
$the_book = new Book($the_title, $the_autor);
return $the_book;
}
.
.
.
So I call it(from the controller) like that:
$book_obj = new Book();
$all_books = $book_obj->getBooks();
$one_book = $book_obj->getOneBook('title');
Or maybe I'm totally wrong and should by in a very different way?
Thank you!
Looking at your two solutions, ask yourself - what happens if you want to start adding more and more types of lookup (by Author, Title, Date). Should they belong in the book model? (a model being a representation of a Book). No would be my answer.
The Book class should be a representation of your Book (Title, Number of pages, Text, etc) - stored in the database in this case.
In my opinion your first approach is your best approach - separate your business logic of looking up a Book into a separate object (following your convention I would suggest something like BookLookupService), which is responsible for looking up a book instance, for example:
interface IBookLookupService {
findBookByTitle($bookTitle);
findBooksByAuthor($authorName);
getAllBooks();
}
Both ways are correct and correspond to two different patterns.
Your first idea could be associated with the Data mapper pattern.
In this case your book class doesn't have to know anything about the database, and let another class worrying about database operations.
This is the pattern use by projects like Doctrine2 or Hibernate (I think)
The second one is known as Active record pattern where you keep everything in a single class and your book object has to manage its own persistence.
This is the pattern used by Ruby on Rails.
The first approach generally give you more flexibility but second one may look more intuitive and easy to use.
Some informations:
http://culttt.com/2014/06/18/whats-difference-active-record-data-mapper/

Implementing a S.O.L.I.D Domain Object Model in the following project

I have the following example in which I tend to use a couple of classes, to create a simple web app.
The file hierarchy seems like this.
> cupid
- libs
- request
- router
- database
- view
- bootstrap.php
- index.php
The index.php just calls the bootstrap.php which in turn contains something like this:
// bootstrap.php
namespace cupid
use request, router, database, view;
spl_autoload_register(function($class){ /* autoload */ });
$request = new view;
$response = new response;
$router = new router;
$database = new database;
$router->get('/blog/{id}', function($id) use ($database, $view) {
$article = $database->select("SELECT blog, content FROM foo WHERE id = ?",[$id]);
$view->layout('blogPage', ['article'=>$article]);
});
As you can probably tell, my problem is this line:
$article = $database->select("SELECT blog, content FROM foo WHERE id = ?", [$id]);
Which I don't want to use, and instead try a " Domain Object Model " approach.
Now, given that I will add another folder called domain, with blog.php
> cupid
- domain
- Blog.php
- libs
...
And fill blog.php with properties mapping table rows, and getter and setters ..
namespace App\Domain;
class Blog {
private $id, $title, $content, $author;
public function getTitle(){
return $this->title;
}
public function setTitle($title){
$this->title = $title;
}
...
}
My question is: Assuming my understanding of DOM is so far correct, and that I have a CRUD/ORM class, or a PDO wrapper to query the database;
"How can I tie together, i.e. the blog model with the PDO wrapper to fetch a blog inside my bootstrap file?"..
As far as a Domain Object you basically already have written one, your blog object. To qualify as a domain model all a class must to is to provide a representation along with any of the functionality of a concept within your problem space.
The more interesting problem here and the one you appear to be struggling with is how to persist a domain model. Keeping with the tenet of the single responsibility principle your Blog class should deal with being a blog post and doing the things that a blog post can do, not storing one. For that you would introduce the concept of a repository of blog posts that would deal with storing and retrieving objects of this type. Below is a simple implementation of how this can be done.
class BlogRepository {
public function __construct(\cupid\database $db){
$this->db = $db;
}
public function findById($id){
$blogData = $this->db->select("select * from blog where id = ?", [$id]);
if ($blogData){
return $this->createBlogFromArray($blogData);
}
return null;
}
public function findAllByTag($tag){...}
public function save(Blog $blog) {...}
private function createBlogFromArray(array $array){
$blog = new Blog();
$blog->setId($blogData["id"]);
$blog->setTitle($blogData["title"]);
$blog->setContent($blogData["content"]);
$blog->setAuthor($blogData["author"]);
return $blog;
}
}
Then your controller should look something like this.
$router->get('/blog/{id}', function($id) use ($blogRepository, $view) {
$article = $blogRepository->findById($id);
if ($article) {
$view->layout('blogPage', ['article'=>$article]);
} else {
$view->setError("404");
}
});
To truly be SOLID the above class should be a database specific implementation of a BlogRepository interface to adhere to IoC. A factory should also probably be supplied to BlogRepository to actually create the blog objects from data retrieved from the store.
In my opinion one of the great benefits of doing this is you have a single place where you can implement and maintain all of your blog related interactions with the database.
Other Advantages to this method
Implementing caching for your domain objects would be trivial
Switching to a different data source (from flat files, blogger api, Document Database Server,PostgresSQL etc.) could be done easily.
You can alternatively use a type aware ORM for a more general solution to this same problem. Basically this Repository class is nothing more than a ORM for a single class.
The important thing here is that you are not talking directly to the database and leaving sql scattered throughout your code. This creates a maintenance nightmare and couples your code to the schema of your database.
Personally I always tend to stick the database operations in a database class which does all the heavy lifting of initialising the class, opening the connection etc. It also has generic query-wrappers to which I pass the SQL-statements which contains the normal placeholders for the bound variables, plus an array of the variables to be bound (or the variable number of parameters approach if thats suits you better). If you want to bind each param individually and not use the $stmt->execute(array()); You just pass in the types with the value in a data structure of your choosing, multi dim array, dictionary, JSON, whatever suits your needs and you find easy to work with.
The model class it self (Blog in your case) then subclasses the Database. Then you have a few choices to make. Do you want to use the constructor to create only new objects? Do you want it to only load based on IDs? Or a mix of both? Something like:
function __construct(id = null, title = null, ingress = null, body = null) {
if(id){
$row = $this->getRow("SELECT * FROM blog WHERE id = :id",id); // Get a single row from the result
$this->title = $row->title;
$this->ingress = $row->ingress;
$this->body = $row->body;
... etc
} else if(!empty(title,ingress,body)){
$this->title = title;
... etc
}
}
Maybe neither? You can skip the constructor and use the new(title, ingress, body), save() and a load(id) methods if thats your preference.
Of course, the query part can be generalised even further if you just configure some class members and let the Database-superclass do the query building based on what you send in or set as member-variables. For example:
class Database {
$columns = []; // Array for storing the column names, could also be a dictionary that also stores the values
$idcolumn = "id"; // Generic id column name typically used, can be overridden in subclass
...
// Function for loading the object in a generic way based on configured data
function load($id){
if(!$this->db) $this->connect(); // Make sure we are connected
$query = "SELECT "; // Init the query to base string
foreach($this->columns as $column){
if($query !== "SELECT ") $query .= ", "; // See if we need comma before column name
$query .= $column; // Add column name to query
}
$query .= " FROM " . $this->tablename . " WHERE " . $this->idcolumn . " = :" . $this->idcolumn . ";";
$arg = ["col"=>$this->idcolumn,"value"=>$id,"type"=>PDO::PARAM_INT];
$row = $this->getRow($query,[$arg]); // Do the query and get the row pass in the type of the variable along with the variable, in this case an integer based ID
foreach($row as $column => $value){
$this->$column = $value; // Assign the values from $row to $this
}
}
...
function getRow($query,$args){
$statement = $this->query($query,$args); // Use the main generic query to return the result as a PDOStatement
$result = $statement->fetch(); // Get the first row
return $result;
}
...
function query($query,$args){
...
$stmt = $this->db->prepare($query);
foreach($args as $arg){
$stmt->bindParam(":".$arg["col"],$arg["value"],$arg["type"]);
}
$stmt->execute();
return $stmt;
}
...
}
Now as you see the load($id), getrow($query,$args) and query($query,$args) is completely generic. ´getrow()´is just a wrapper on query() that gets the first row, you may want to have several different wrappers that to or interpret your statement result in different ways. You may also even want to add object specific wrappers to your models if they cannot be made generic. Now the model, in your case Blog could look like:
class Blog extends Database {
$title;
$ingress;
$body;
...
function __construct($id = null){
$this->columns = ["title","ingress","body","id",...];
$this->idcolumn = "articleid"; // override parent id name
...
if($id) $this->load($id);
}
...
}
Use it as so: $blog = new Blog(123); to load a specific blog, or $blog = new Blog(); $blog->title = "title"; ... $blog->save(); if you want a new.
"How can I tie together, i.e. the blog model with the PDO wrapper to fetch a blog inside my bootstrap file?"..
To tie the two together, you could use an object-relational mapper (ORM). ORM libraries are built just for glueing your PHP classes to database rows. There are a couple of ORM libraries for PHP around. Also, most ORMs have a built in database abstraction layer, which means that you can simply switch the database vendor without any hassle.
Considerations when using an ORM:
While introducing a ORM also introduces some bloat (and some learning), it may not be worthwhile investing the time for simply a single Blog object. Although, if your blog entries also have an author, one or multiple categories and/or associated files, an ORM may soon help you reading/writing the database. Judging from your posted code, an ORM will pay off when extending the application in the future.
Update: Example using Doctrine 2
You may have a look at the querying section of the official Doctrine documentation to see the different options you have for read access. Reconsider the example you gave:
// current implementation
$article = $database->select("SELECT blog, content FROM foo WHERE id = ?",[$id]);
// possible implementation using Doctrine
$article = $em->getRepository(Blog::class)->find($id);
However, ideally you define your own repository to separate your business logic from Doctrines API like the following example illustrates:
use Doctrine\ORM\EntityRepository;
interface BlogRepositoryInterface {
public function findById($id);
public function findByAuthor($author);
}
class BlogRepsitory implements BlogRepositoryInterface {
/** #var EntityRepository */
private $repo;
public function __construct(EntityRepository $repo) {
$this->repo = $repo;
}
public function findById($id) {
return $this->repo->find($id);
}
public function findByAuthor($author) {
return $this->repo->findBy(['author' => $author]);
}
}
I hope the example illustrates how easily you can separate your business domain models and logic from the underlying library and how powerful ORMs can come into play.

Dynamic doc blocks for type hinting and autocomplete with dynamic output classes

I have a medium-sized MVC application. The use cases for the app are such that I require a bit more control of the SQL used to interact with my database (among other things, data is highly normalized and we use a LOT of joins). So, I've grown my own design for models, rather than using Doctrine or Eloquent or anything. I recently discovered my design is called the Table Data Gateway pattern. At any rate, it looks like this (simplified a bit for clarity):
We have an an abstract class which all my TableGateways extend. It knows how to talk to PDO and do SQL stuff, but not about any particular table:
abstract class TableGateway {
protected $db = null;
public $tableName = '';
public $outputClass = null;
function __contruct(\PDO $pdo) {
$this->db = $pdo;
}
/**
* $return \StdClass[]
*/
function all() {
$result = $this->query(sprintf('SELECT * FROM %s',$this->tableName));
return $result;
}
/**
* #param $query
* #param array $params
* #return \StdClass[]
*/
public function query($query, $params = array()) {
$stmt = $this->db->prepare($query);
if($this->outputClass) {
$stmt->setFetchMode(PDO::FETCH_CLASS, $this->outputClass);
} else {
$stmt->setFetchMode(PDO::FETCH_OBJ);
}
$success = $stmt->execute($params);
return $stmt->fetchAll();
}
}
Then each table has a gateway class which extends the parent class and applies the table-specific stuff. There will be a lot of functions in here which call $this->query(), bringing their knowledge of the table's structure to build a useful query, but there are also some basic functions, like PostsGateway::all() which are the same for every table, so they live in the parent class.
class PostsGateway extends TableGateway {
public $tableName = 'posts';
public $outputClass = 'Post';
}
Each result from the query is loaded into an object. By default they are just StdClass objects, but you can specify an alternate $this->outputClass to have them output in that other class, like this one:
class Post {
/** #var int $id */
/** #var string $content */
}
All this is working pretty well for me. However, my IDE can't keep up. You and I know that PostsGateway::all() is going to return an array Post objects, but the IDE thinks it's just a regular array. This means I don't autocomplete hints for the properties of the Post objects.
Does anyone know any clever ways to resolve this, so the IDE will know about the change?
I was thinking about a script to automatically generate doc blocks for PostsGateway, but I'm a little leary of code gen in general, and especially code gen in a class I'm going to modify periodically. The script would have to periodically update the doc blocks, based on changes to TableGateway, and not mess up anything I've added to PostsGateway.
Are there any other cool ways to do get around this? Or can you recommend some good libraries for generating this code blocks?

Categories