So I was messing around with the STL the other day, mainly to compare OOP in PHP and in C++ and I came up with two simple comparisons.
PHP: Plain <?php class Person implements ArrayAccess { public $name; public $properties = array(); public function __construct($name) { $this->name = $name; } public function offsetGet($key) { return $this->properties[$key]; } public function offsetSet($key, $value) { $this->properties[$key] = $value; } public function offsetExists($key) { return isset($this->properties[$key]); } public function offsetUnset($key) { unset($this->properties[$key]); } } $shawn = new Person("Shawn"); $shawn["last_name"] = "Biddle"; echo $shawn->name . " has the following properties:\n"; foreach($shawn->properties as $key=>$value) { echo "\t$key = $value\n"; } ?>
This is in comparison to the C++ version: Plain #include <iostream> #include <string> #include <algorithm> #include <map> using namespace std; class Person { public: Person(string name) : name(name){} string &operator[](const char* key) { return this->properties[key]; } inline static void prettyPrint( pair<string, string> prop ) { cout << "\t" << prop.first << " = " << prop.second << endl; } string name; map<string, string> properties; }; int main(int argc, char** argv) { Person* shawn = new Person("Shawn"); (*shawn)["last_name"] = "Biddle"; cout << shawn->name << " has the following properties:" << endl; for_each( shawn->properties.begin(), shawn->properties.end(), &Person::prettyPrint ); delete shawn; return 0; }
In both examples I create a class Person. In the PHP example the class must implement the ArrayAccess interface to use array notation on the class, this is achieved in the C++ example by overloading the [] operator. If I was scoring this comparison I'd chalk one up for C++ since requiring the implementation of an interface means we're defining a bunch of unnecessary functions.
The reason I create the ability to use [] notation on it is for access to the properties property of the class. Now, if you notice both are associative arrays. This is not something built-in to C++ so I use map from the STL to achieve the same functionality.
The next thing I do in both examples is iterate through each of the Person instance's properties while executing a bit of code. Now, once again, if I was scoring this I would chalk one up for PHP simply because of the ease of foreach. However, as you can see, I achieve something similar in C++ by using the for_each function from the "algorithm" library. My main gripe with this is that C++ has no lambdas so I must define a function and pass a pointer to that function to for_each with the parameter &Person::prettyPrint.
Having said that I feel it necessary to mention that C++0x does have lambdas along with a foreach(called range-based for loop in the standard), in which case it would look something like this: Plain // with a range-based for for(pair<string, string> prop : shawn->properties) { ... } // or using a lambda for_each( shawn->properties.begin(), shawn->properties.end(), []( pair<string, string> prop ) { ... });
Now, I'll be the first to say that I hate the syntax of C++0x's lambdas. To a lesser extend the range-based for as well.But I digress, overall it is very similar. As usual, PHP requires you do jump through some hoops to let you do what you want but you can get it done, and C++ lets you do what you want with the drawback of decrypting its oversalted syntax. So, the the biggest difference which I didn't go over here is the lack of multiple in heritance in PHP, which frankly, is painful. Moreover, though it goes without saying, there is the performance, code size, and executable size difference (C++ winning them all.)