ClassicTeck

ckMessage (ckmsg)

CKMSG is a I'm bore kind of project. CKMSG adds an Object Oriented BASIC syntax to PHP. CKMSH is inspired by Objective-C.

Why CKMSG? Well I was bore, besides that I wanted to play with Messaging like OOP. The main difference of CKMSG's OOP and PHP OOP is exactly that "messages". In PHP and other C++ like languages you call methods of a class in the other hand languages like SmallTalk, Objective-C and CKMSG send messages to a class. In traditional OOP complex problems are broken down using abstraction with classes that encapsulate and hide the implementation. This structure makes it easier to reuse code. In the other hand inheritance in traditional OOP is not so flexible and in many situations it lends itself to produce an eternal hierarchy of classes. Each class offering a tiny subset of specialization just enough to make it reusable. In all this inheritance madness we forget why we started using OOP and all the sudden we lost all simplicity. In contrast delegation and messaging are paradigms that allows us to create the abstractions of OOP while offering clean and simple means to add functionality to our objects and specialize them as needed.

In CKMSG I wanted to loose the traditional class notion for a model. A model is a sort of prototype metaclass that implements a behavior by means of messages and properties. Just like in prototype languages the model is a very dynamic data type that can be shaped at runtime. Every model is a metaclass and an instance of a metaclass is simply a clone of a behavior. In that sense CKMSG Objects behave more like Javascript's Objects.

Finally in CKMSG I also added a subset of BASIC syntax and control structures.

Example Model

model $a
$a.dothis()

'explicit method binding for model a
method a::dothis()
    print "hello"
end method

Download

Requires: PHP 5. No extensions or modules required.
Source Code Last Updated: Dec/30/2009

Models


To declare a model:
	model $model_name
	model $model_b extends $model_c
A model name is global in scope and there can not be two models of the same name.

To declare the behavior of a model we define methods that responds to a particular message. The method name corresponds to a message name that can be send to a model. Each method is associated with a model using the notation model_name::message_name in its definition.
	method model_name::message_name( [formal_parameters...] )
		[statements...]
	end method
Sending a message to a model
	$myModel.message_name( [parameter1[, parameter2, ...]] )
Since messages are resolved at runtime, sending a message does not implies that it will be handled. If a message is not resolved to a behavior it will be send to the object which acts as a proxy for all the models. The object always returns a null for any message that it can not handle.

Initializing a model

Some models may need to be initialized before using them. We do so with a special message called "init".
model $a
model $b extends date
model $c extends b

$a.init()
$a.dothis("jose")

$c.init()
print $c->longDate & "\n"

'explicit method binding for model a
method c::init()
        print "called C init\n"
	$super.init()
end method
method b::init()
        print "called B init\n"
	$super.init()    
end method

Binding a model to another


When we bind a model to another we can have messages sent from one model also send to the other. A model can have unlimited models binding to it. With binding we can make a model act as a proxy or generic responder. Binding allows a model to have multiple models acting as delegate.
model $a
model $c

if $a.respondsToMessage("dothis") then
    $a.dothis("jose")
end if

$c.setBinding($a)
$c.dothis("joe")

method a::dothis($name)
	print "hello {$name}"
end method
When a message is received by the model it will forward the message to all the models binded to it. If the model itself implements the message it will invoke the message after all the other models have executed the message and the result value of its own message is returned if any. If the model does not implements the message it will return a list with whichever values were returned by each binded model.
model $a
model $b
model $c

$c.setBinding($a)
$c.setBinding($b)
$list = $c.dothis("joe")
print_r($list)
$c.removeBinding($b)

'explicit method binding for model a
method a::dothis($name)
	print "hello {$name}"
	return $name
end method
method b::dothis($name)
	print "hello {$name}"
	return "hello {$name}"
end method

Object Messages


All instances inherit from "object". Messages implemented by Object are:
	$model.model() as string
	$model.prototype() as string
	$model.objectname() as string
	$model.self() as model
	$model.isBasedOnModel($model_name as string) as boolean
	$model.respondsToMessage($message_name as string) as boolean
	$model.count() as integer
	$model.isempty() as boolean
	$model.setBinding($aModel as model)
	$model.removeBinding($aModel as model)

Variables

dim $name as string
$name = "Jose Cuevas" 
dim $i as integer
$i = 5
dim $d as double
$d = 5.5
dim $b as boolean
$b = true
dim $s as string, $j as string, i as integer

Control Structures

dim $i as integer
$i = 2
if $i == 0 then
    print "hello1"
elseif ($i == 1) then
    print "hello2"
end if
dim $i as integer

while $i < 10
	$i = $i + 1
	print "{$i}"
wend
print "i is now={$i}"
dim $i as integer
for $i = 0 to 10
	print "{$i}"
next
print "i is now={$i}"

for $i = 10 downto 0
	print "{$i}"
next
print "i is now={$i}"
$s = array("name"=>"jose", "n"=>"joe" )

foreach $v in $s
	print "{$v}<br>"
next
 $s = array("name"=>"jose", "n"=>"joe" )

foreach $k=>$v in $s
	print "{$k}={$v}"
next
$i = 5
select case $i
case 1
    print "is one"
case 2, 3
    print "is two or three"
else
    print "is something else"
end select

Framework


Date Model

	model $d extends date
	$d.init()

Properties

	model $d extends date
	$d.init()

	print $d->year & "<br>";
	print $d->month & "<br>";
	print $d->day & "<br>";
	print $d->hour & "<br>";
	print $d->minute & "<br>";
	print $d->second & "<br>";
	print $d->weekOfYear & "<br>";
	print $d->dayOfWeek & "<br>";
	print $d->dayOfYear & "<br>";
	print $d->totalSeconds & "<br>";
	
	print $d->shortTime & "<br>";
	print $d->longTime & "<br>";
	print $d->shortDate & "<br>";
	print $d->longDate & "<br>";
	print $d->abbreviatedDate & "<br>";
	
	print $d->SQLDate & "<br>";
	print $d->SQLDateTime & "<br>";

Methods

	model $d extends date
	$d.init()
	$d.setTotalSeconds( $date->now() );
	
	$t = $d.now();
	
	$d.format("Y-m-d H:i:s")