Posts Tagged ‘OOP’

ArgumentList 0.1

Friday, January 30th, 2009

Some times ago I was reading the book “An Introduction to Design Patterns in C++ with Qt 4” and I found really useful for lazy peoples like me the utility for easily access command line argument list parameters in C++ with the use of QT framework.

So I decided to write from scratch a similar code, but in pure C++.

Using it is quite simple, as you can read in the test available in the package and here:

#include "ArgumentList.h"
#include

using namespace sclocco;
using std::cout;
using std::cin;
using std::endl;
using std::string;

int main(int argc, char *argv[]) {
ArgumentList args = ArgumentList(argc, argv);
string input = "";
char again = 'Y';

cout << "Testing utility for ArgumentList class." << endl;

while ( again == 'Y' ) {
cout << "Do you want to search for a switch, for the argument of a switch or the first remaining argument (1/2/3): ";
cin >> input;

if ( input.compare("1") == 0 ) {
cout << "Insert the switch: ";
cin >> input;

if ( args.getSwitch(input) ) {
cout << "Switch \"" << input << "\" found." << endl;
}
else {
cout << "Switch\ "" << input << "\" not found." << endl;
}
}
else if ( input.compare("2") == 0 ) {
string temp = "";

cout << "Insert the switch: ";
cin >> input;
temp = args.getSwitchArgument(input);
cout << "The argument of switch \"" << input << "\" is: " << temp << endl;
}
else if ( input.compare("3") == 0 ) {
cout << "The first remaining argument is: " << args.getFirst() << endl;
}

cout << "Continue testing (Y/N): ";
cin >> again;
}

return 0;

}

If you want to try it download the package here.

MySQL class 0.2

Tuesday, August 5th, 2008

During March I wrote a post about a simple class that I use when I need to communicate with a MySQL database in PHP 5.
Now it’s time to release version 0.2 of that class.

There are two changes from the past version:

  1. some bugs fixed;
  2. added a new method named “count“.

The new method is designed to count the occurrences of “something” “somewhere” so when you need to do something like:

SELECT COUNT(some columns) FROM some tables WHERE some conditions;

you can simply type (assuming you have created a $database object before):


$database->count("some columns", "some tables", "some conditions");

Enjoy!

Download the MySQLDB class source.

MySQL class 0.1

Saturday, February 23rd, 2008

Some times ago I wrote that simple class in PHP (keep in mind: it’s not compatible with PHP4) for talking with a MySQL database and now that I’ve used it a little bit in some projects i want to share the code with all of you.

First of all let me say that probably you can use for all life the classical mysql functions and not suffer of any “damage” and obviously I use those functions inside my own class, but if you want to write code in a more “OO way” and a more readable code too, you may give a try to that class.
Oh, don’t worry, I don’t go mad if you don’t want or don’t like my class.

So, let’s stop with that insane writing and let me introduce briefly how class work in a user perspective, you can read all the source code until you’ve downloaded the source file.

First of all we need to construct an object of type MySQLDB:

$database = new MySQLDB();

There are three possible parameters you can pass to the constructor: username, password and hostname for accessing the server; those parameters are respectively, in case you leave them empty, an empty string for the first and second and “localhost” for the third.
You can easily change that parameters later using methods like setUser(), setPassword() or setServer() but I think it’s easier to pass first and second to the constructor instead:

$database = new MySQLDB("username", "passoword");

Obviously I’m talking in the eventuality that your DBMS is located on localhost.
If username, password and hostname are correctly given to the object you can connect to the database:

$database->connect();

You can at any time change database doing something like that:

$database->disconnect();
$database->setUser("newuser");
$database->setPassword("newpassword");
$database->setServer("newserver");
$database->connect();

Yeah, maybe you can write few rows of code doing by yourself, but if you don’t need to change all parameters it can be cool.
When you’re connected you can access a database in that way:

$database->setDatabase("database");

For setting and executing a query:

$database->setQuery("QUERY ...");
$database->query();

Maybe i had had to advert you that some of this methods can throw exceptions, so, for your safety is better if you use try & catch blocks that my laziness prevents me to write right now in that examples :)
If you want to retrieve data from a select type of query or maybe know how much rows you deleted/updated and things like that you can use that powerful duo: getResultsNumber() and getData().
A couple of examples can explain how them work:

//  I've done an insert before and want to know if all was well

if ( $database->getResultsNumber() == 0 ) {
print("Something bad happened!");
}
// I've done a select between multiple rows and now want to use the data collected

for ( $i = 0; $i < $database->getResultsNumber(); $i++ ) {
$temp = $database->getData();
/* do something with the object $temp that has an attribute for every column you put in the select */
}

No need to call a special method for closing connection or cleaning the state, the destructor makes all the work for you!
What can I say more ?
Maybe you can write yourself or download a better MySQL class, but as I said, I’ve wrote that class and I prefer to share code than lost it on a remote directory of my computer.
Last thing to say is that MySQLDB class is released under GNU GPL version 3 license and that you can write me, or contact me in the way you prefer, for every question/comment.

Download the MySQLDB class source.

Page class

Tuesday, November 7th, 2006

Simple class to represent a web page with template support, the class is so simple that if you are a coder you can easily read it (sorry for missing comments).

If you are not interested with the implementation I’ll write here a simple explanation of how it works.

Obviously you have to include the file into your code with something like:

require("page_class.php");

After you can instance an object in a really simple manner:

$page = new Page("include/template.tpl");

As you can see the only parameter of constructor is the path of a template page; some methods (and constructor also) can return an exception if something goes wrong, you have to use try and catch.
Probably you want to insert some dynamically generated content into the page, so let’s make the assumption that your template contains some HTML code and some placeholders.
The first thing to do is to select left and right placeholder’s marker:

$page->setPlaceholder("<_?", "?_>");

Now my placeholders have general shape like that: <_?NAME?_>.
If you want to insert some content for replacing a placeholder:

$page->setContent("isazi", "NAME");

The page now has every occurrence of <_?NAME?_> replaced with string isazi.
Maybe in the future i can insert a third parameter if you want to replace only a selected number of placeholders, but this is another story…
Only two methods remain that are useful for everyone, and are:

$page->getPage();

If you want to get the whole page and save it into a string and:

$page->show();

If you want to print the page.

If you want the source, you can download it.