Stratum is a php-like class system for lua with inheritance, interfaces, traits and statics, exceptions and try/catch functions.
- Class inheritance, interfaces and traits.
- Exceptions and try/catch functions.
- PHP-style definitions using syntactic sugar.
- No dependencies.
- Private/public/protected properties/methods.
require('stratum/stratum')
Below are a few examples on how to use stratum.
class 'User' is {
__construct = function(self, name)
self.name = name
end,
sayHello = function(self)
return string.format('Hi %s!', self.name)
end
}
local user = new 'User' -- Without parameters
local user2 = new ('User', 'Adam') -- With parameters
class 'Administrator' extends 'User' is {
sayHello = function(self)
return string.format('Hi %s, you\'re an administrator!', self.name)
end
}
local admin = new ('Administrator', 'Jim')
print(user:sayHello()) -- Hi Adam!
print(admin:sayHello()) -- Hi Jim, you're an administrator!
interface 'DataInterface' is {
getData = function() end,
setData = function() end
}
class 'TableDataInterface' implements 'DataInterface' is {
data = {},
getData = function(self, table)
return self.data[table] or {}
end,
setData = function(self, table, data)
self.data[table] = data
end
}
trait 'Shareable' is {
share = function(self, text)
self:original():share('Trait! ' .. text)
end
}
class 'Post' has 'Shareable' -- The `is {}` declaration is missing here because there is no class body
class 'Comment' has 'Shareable' is {
share = function(self, text)
print('Sharing ' .. self.__className .. ': ' .. text)
end
}
local post = new 'Post'
post:share('I just shared a post with stratum!') -- Errors, there is no share method in the Post class
local comment = new 'Comment';
comment:share('I just shared a comment with stratum') -- The trait calls the share method with some text prepended
local User = class 'User' is {
exists = false,
lastID = 1,
create = function(name, email, password) -- static method, no self argument
local user = new 'User'
user.name = name
user:save()
return user
end,
save = function(self)
-- Save to the database?
self.exists = true -- instance property
self:static().lastID = self:static().lastID + 1 -- static property
return true
end
}
local user = User.create('Test Name') -- User instance
print(User.lastID) -- Static property
class 'SomeClass' extends 'AnotherClass' implements 'SomeInterface' implements 'AnotherInterface' has 'SomeTrait' has 'AnotherTrait' is { }
class 'SomeException' extends 'Exception' is { }
class 'SomeOtherException' extends 'Exception' is { }
try(function()
throw ('SomeException', 'This is a message')
--echo something[thatDoesNotExist]
end)
catch({
['SomeException'] = function(exception)
return('Caught SomeException: ' .. exception.message) -- This will be called
end,
['SomeOtherException'] = function(exception)
return('Caught SomeOtherException: ' .. exception.message) -- This won't be called
end,
['ErrorException'] = function(exception)
print('There was an error!' .. exception.message)
end
})
There are a few performance and test files included in the tests
directory. These tests also contain working examples of each feature of Stratum.
If you prefer your lua a little less sweet, you can also use standard syntax.
class('User')
extends('SomeClass')
impliments('SomeInterface')
has('SomeTrait')
is({ someProperty = 'hello', someMethod = function(self) return self.someProperty end })
new('User')
throw('Exception')
try(function() end)
catch('Exception', function() end)
Stratum defines the below 8 global methods.
class(name)
interface(name)
trait(name)
extends(name)
implements(name)
has(name)
is(tbl)
new(class, ...)
throw(class, ...)
try(function)
catch(exception, function) or catch(table)