Tutorials


Simulate Bitwise Shift Operators in Lua

EDIT: Here's a couple functions which will do the shift operations that I've put in gist #938502.

The fact that Lua doesn't have bitwise operators is a pain. There are pure Lua implementations out there, such as LuaBit, but I find these to be a little slow (I think LuaBit using tables or something like that). I've found a way to simulate both the left-shift and right-shift operator by using some simple mathematics.

Read more

Mixin Inheritance in MiddleClass

The technique I'm about to present, may seem obvious, but I'll share it anyway. The way I would create mixins that inherit stuff from other mixins is this:

Mixin = {}

function Mixin:included(class)
  if not includes(ParentMixin, class) then
    class:include(ParentMixin)
  end
end

When the mixin is included, it will check whether the class includes the mixin, and if not, it will include it, therefore simulating inheritance. It's good to check if the class includes the parent mixin, because MiddleClass does not do this itself.

Read more

Singleton Class in MiddleClass (Lua)

I thought I'd demonstrate a method for making a singleton class in MiddleClass. If you don't know, MiddleClass is an object-orientation library for Lua.

So why would you want to make a class that only has one instance with MiddleClass? Couldn't you just use a table? Well, making it a class in MiddleClass allows you to take advantage of a number of other cool things that MiddleClass has on offer, like mixins, inheritance and so on. So let's have a look at the method.

Read more

Generating a Joomla Password (for reseting)

I recently found myself in the awkward situation of having only a Super Admin user in the Joomla installation, and neither I nor my client knew the password. I had to the fix the problem, and I decided to do so by generating a Joomla user password (which required a lot of searching through code, mind you). Here's the PHP code I used:

<?php
// JUserHelper class from libraries/joomla/user/helper.php
$pwd = 'my_new_password';
$instance = new JUserHelper();
$salt = $instance->genRandomPassword(32);
$final_pwd = $instance->getCryptedPassword($pwd, $salt) . ':' . $salt;
?>

(Note that I have to use the <?php tags to get syntax highlighting to work properly.)

Read more

Making a Dynamic Copyright With PHP

I though I'd just write a quick tutorial on how I make a dynamic copyright for my websites. If you don't know what a function does in the code, look it up on the PHP manual.

<?php echo strftime('%Y', time()); ?>

That's the year generating code. It's just grabbing the year from the current timestamp. So we could do something like this in the copyright:

&copy; <?php echo strftime('%Y', time()); ?> Legion of Weirdos.

You might want to wrap around the code in a helper function:

Read more