Looking for more information on how to do PHP the right way? Check out PHP: The Right Way

Ibuildings Blog:
Working with Entities in Drupal 7
May 05, 2016 @ 12:29:26

On the Ibuildings site there's a tutorial posted talking about working with entities in Drupal 7 and how creating your own classes for them can make them easier to manage.

Developers love object-oriented code. But how can this be achieved with Drupal 7 entities? By default Drupal uses a single class for all entities of a given type. For example, all node objects are standard classes (stdClass) and all entity objects have the Entity type. I personally like to have an entity model that only exposes the functionality that is applicable for the logic of your domain.

[...] Wouldn’t it be nice to create your own classes for entities?

First off, he starts with a refresher on what entities are and how they relate to the database schema. He points out the difficulties in using them and testing their types. He then provides his suggested solution for "all" of your entity problems - the creation of classes for the different entity types. He gives an example using an Article type and how to create/use them in your Drupal code.

tagged: drupal7 entities custom class stdclass tutorial

Link: https://www.ibuildings.nl/blog/2016/04/working-entities-drupal-7

Jani Hartikainen's Blog:
Base classes in OOP programming languages
Nov 19, 2008 @ 16:10:58

Jani Hartikainen has followed up on a post from David Otten about standard classes in PHP and how they provide the base for much of what the language does.

David Otton posted a short but thought-provoking post about stdClass, which many think is the “base class” all PHP classes automatically inherit from. I have to admit that I had this misconception as well. [...] This [difference in PHP from other OOP languages] presents some room for analysis in how things are handled in dynamic and static languages, and how those differences affect things...

Jani talks about dynamic and static typing in languages and how that effects the base types things are extended from as well as some of the benefits that having a standard base class affords developers.

tagged: base class programming language oop stdclass standard inherit


David Otton's Blog:
PHP Tip: Classes Aren’t Derived From stdClass
Nov 16, 2008 @ 10:54:53

David Otton has shared a discovery he's come across in his development - user-defined classes are not derived from stdClass.

Many OO languages have the concept of a single base class from which all other classes are explicitly or implicitly descended. For example, Ruby, Java and .NET all have Object. It's a very common belief that PHP implements stdClass as a base class for all objects, but this is in fact not the case.

He illustrates with a code example showing the results of calls to instanceof with a normal user class and one that extends the stdClass object.

tagged: class stdclass standard example instanceof