April 8, 2008
-
e-commerce standard field names. Use them..
April 8, 2008
April 3, 2008
April 1, 2008
March 18, 2008
March 11, 2008
February 26, 2008
It’s three articles (1 2) now, it’s almost a book! Here’s how to create a class in my three favorite languages:
The advantage of this method is it’s obvious portability to other (e.g. compiled) languages. If you add getters and setters the link to java is easily made.
colors = 'red,green,blue,yellow,white,orange'; $c->size = 4; $c->brand = 'rubiks'; foreach (get_object_vars($c) as $property=>$value) print_r($property . ' = ' . $value . '<br />'); ?>
The advantage of this method is the fact that properties (any method or attribute) can me created on the fly, even using variables, e.g. ‘price’, ‘new’ + ‘price’ or even ‘new’ + ‘price’ + $date.
$prop = 'red,green,blue,yellow,white,orange'; $prop = 'size'; $c->$prop = 4; $prop = 'brand'; $c->$prop = 'rubiks'; foreach (get_object_vars($c) as $property=>$value) print_r($property . ' = ' . $value . '<br />'); ?>
The advantage of this method is that the object doesn’t actually have a class. Because of that properties can be invented at will, and they don’t have to be variables, shortening the code to a form that looks like the class actually was defined. Be careful with this though, other people who read your code will frantically look for the class definition and won’t find it if they don’t know about the use of stdClass. An equivalent, but not as nice looking, is to cast null to an object, as in “$c = (object) null;”
colors = 'red,green,blue,yellow,white,orange'; $c->size = 4; $c->brand = 'rubiks'; foreach (get_object_vars($c) as $property=>$value) print_r($property . ' = ' . $value . '<br />'); ?>
In javascript there are plenty of possibilities for creating objects. Classes however do not exist (not in a practical sense anyway). Any object you create is a ‘class’ in itself, and can be copied or extended on the fly.
Note how javascript’s for..in actually iterates over all property names (not the values as you might expect).
var c = new Object; c.colors = 'red,green,blue,yellow,white,orange'; c.size = 4; c.brand = 'rubiks'; for (property in c) alert(property + "=" + c[property]);
Though it is correct that arrays and functions are both objects, and it seems an easy way of providing php’s foreach construct, the for..in is not suitable for this. An array with 10 elements, with index 0..9 cannot be iterated this way, since the array is also a ‘Class’ and all its properties, e.g. the length property, will also be iterated. In the example below, if we added a method ’show’, we would see the definition of the function, converted to a string. That’s right, the full source code of the method.
r = Array('red','green','blue');
r.show = function() { alert(this); }
for (property in r)
alert(property + "=" + r[property]);
I will not consider VB.Net, since in .NET every program is object oriented (supposedly) and thus classes are at the base of the language. Anyway, it’s not more difficult than choosing “add item > class”, and almost all the code is created for you.
In Visual Basic it get’s more interesting, since it’s not an OO language in itself. Again, just like in JavaScript, objects are more considered ‘containers for variables and functions’. The concept of subclassing does not exist in VB. OK, here it goes. I am assuming you have created a Standard Exe project. First, choose Project > Add Class Module. You’ll see a new module created named ‘Class1′. You can just rename that in the properties pane to Cube. Now add the code:
' method 1: public members Public colors As String Public size As Integer Public brand As String
This is not the preferred way, we should actually create getters and setters for these. This is easy using Tools > Add Procedure. Name it colors e.g. and you will get the following code
Public Property Get colors() As Variant End Property Public Property Let colors(ByVal vNewValue As Variant) End Property
To this you’d still have to add a private variable, such as m_colors, which would be used in your Get and Let methods. You can create a new Cube object from within a form or a module using “c=new Cube”.Finally, in VBScript you could either create a class definition as in VB (without the type names though)… or use the ExecuteGlobal method to evaluate code and, again, generate classes on the fly.
February 21, 2008
February 20, 2008
February 15, 2008
February 13, 2008