Archives

Block undefined properties on PHP classes

Coming from C#, I am not really used to being able to define properties on the fly. In C# you have to declare basically everything. Not in PHP. You can define a class and do whatever you want with it. Explicitly declaring a property actually has very little use. For example, the following is perfectly legal.

class MyObject
{
}

$obj = new MyObject();
$obj->Username = "John Doe";

Now i’m a bit of a strange guy, and I actually DON’T want this behaviour on my framework classes. What I would like is a compiler-time error like ‘Undefined Property’, but since PHP isn’t compiled that gets slightly difficult. Instead, I came up with the following concept called a trait:

trait SolidObject
{
        //this is a guard to block undefined variables.
        public function __get($name)
        {
                if(!property_exists($this, $name))
                {
                        throw new UndefinedPropertyException(get_class($this).'.'.$name);
                }
        }

        public function __set($name, $value)
        {
                throw new UndefinedPropertyException(get_class($this).'.'.$name);
        }
}

Normally, when an undeclared property is used, the PHP engine calls the __get or __set method on a class. PHP allows you to override these methods (Basically it blocks all access to a __get, i don’t even think the property_exists is necessary) But instead of having to extend from a base class they have a thing called ‘traits’. Traits are comparable to extension classes like you have in C#, Swift and Objective C.

Here’s an example on how to use it:

class MyObject
{
   use SolidObject;
}

$obj = new MyObject();
$obj->Username = "John Doe"; 

Now if you run this, the code will throw an exception at runtime, which is good enough for me.

Notice that unlike properties, access to undefined variables can be blocked by including the E_STRICT flag on the error_reporting call, or simply use E_ALL like this:

error_reporting(E_ALL);

Perhaps a future version of PHP will allow typed variables and properties for even more goodness.

 

Sending mail using PHP templates

For the Sensoduino monitor website ‘Sensorium’ i’ve implemented email notifications. These HTML message are sent whenever a trigger is activated. To avoid endless prototyping I wanted to use the php engine itself to come up with the email.

Sensorium_trigger_alarm_for_Moisture_Level_of_Sample_HUB_—_Trash

I made a php page named ‘NotificationTemplate.php’ and added some code to fill a few default variables. Also, the template includes shared header and footer file to allow the mail template to be reused.

<?php

require_once __DIR__.'/../config.php';

if(!isset($triggerInfo))
{
        class dummyData
        {
                public $TriggerID = "X";
                public $SensorDescription = "[SENSOR NAME]";
                public $Threshold = "[THRESHOLD]";
                public $MetricDescription = "[METRIC NAME]";
        }

        $triggerInfo = new dummyTrigger();
        $active = true;
        $triggerType = "[IS ABOVE]";
        $logoImageSrc="../images/Sensorium@2x.png";
        $poweredByImageSrc="../images/PoweredBySimplyduino@2x.png";
}

$url = HOME_URL . '/notifications.php';

include 'MailHeader.php';
?>

<p>
Hi,
</p>
<p>
The sensor <b><?php echo $triggerInfo->SensorDescription; ?></b> has reported that the <b><?php echo $triggerInfo->MetricDescription;?></b> <?php echo $triggerType;?> <?php echo $triggerInfo->Threshold; ?>.
</p>
<p>
Sensorium.
</p>
<?php

include 'MailFooter.php';

?>

To use this template, it needs to be ‘rendered’. Below is a fragment from my ‘sendNotification’ method that initialises a few variables and renders the above template file to a string:

$logoImageSrc = "cid:logo_sensorium";
$poweredByImageSrc = "cid:logo_powered";

//construct the message by loading running a template
ob_start();
        
require 'Templates/NotificationTemplate.php';
        
$message = ob_get_contents();
ob_end_clean();

 

The $logoImageSrc variable is used to switch between inline image attachments. As you can see in the template it is given a default if $triggerInfo was not defined.

This way the message can be preview in the browser by directly opening the php file in Safari. I am still figuring out a way to automatically convert the inline images to attachment because it will clean up the code.

Now that we have the messagebody captured in $message, we can easily send the mail by the php mail() method, or even better use phpMailer.

Sensorium for Simplyduino

As a way to demonstrate the possibilities of the Simplyduino I am writing a monitoring website called ‘Sensorium’ (the hub for all sensors).Below are a few screen shots of the mockup site.

Sensorium_-_Index

The app relies on an Arduino rigged as a bridge-node that forwards the sensor metrics to the website using http requests. This way the system does not need any complete USB or Serial port handling, which makes it possible to run the website on any server in the world.

Sensorium will send notifications (by email) when one of your sensors reaches a configurable state. In a future version these notification should also be able to send out triggers to drive the output drivers of the Sensoduino, for example to turn on a light when garage door opens. Now I realise this is a bad example and could be better done by the Arduino directly, but this approach makes it possible to have complex and configurable logic taken outside the Arduino and into the ‘internet of things’.

Sensorium_-_Chart

Check here for more info about the Simplyduino project.

Simplyduino sees first light

Today my new personal Arduino clone ‘Simplyduino‘ saw its first light of day. It’s blinking!! In the video below this little board is still on life support, but they will soon be removed! Blinky blink!

Check here for on the project details.