/log

life.log()

Archive for the ‘design patterns’ tag

Singleton mySQL database connection using PHP

with 9 comments

السلام عليكم و رحمة الله و بركاته
فيما يلي مثال على الـ Singleton Design Pattern
و هو ما يعطيك الإمكانية في الحصول علي فقط نسخة واحدة من أي مورد تريد إستخدامه .. في هذة الحالة مثلا الإتصال بقاعدة البيانات و ذلك بأن تستخدم الدالة getConnection بدلا من عمل نسخة جديدة من connection كل مرة هتحاول فيها الإتصال بقاعدة البيانات..

Following is a brief example for the singleton design patterns which will enable you from having only one instance of the resource you are controlling .. in this example it’s the mySQL database connection.
Else you will have multiple instances of the same class “connection” each time you want to connect.

define('DB_NAME', "test");
define('DB_USER', "root");
define('DB_PASS', "rootpass");
define('DB_HOST', "localhost");

class Connection{
	private static $connection_;

	private function __construct(){
		$con = mysql_connect(DB_HOST, DB_USER, DB_PASS);
		mysql_select_db(DB_NAME, $con);
		Connection::$connection_ =  $con;
	}

	public static function getConnection(){
		if( !Connection::$connection_ )
			new Connection;
		return Connection::$connection_;
	}

}

$con = Connection::getConnection();
و نسألكم الدعاء بظهر الغيب بالله عليكم :)
في رعاية الله و أمنه

Written by me

June 29th, 2010 at 11:57 am

yield statement .. by example

with 2 comments

السلام عليكم و رحمة الله وبركاته

ده تمرين بسيط علىyield statement

def simpleGenerator( anyList ):
	for item in anyList:
		yield item

myList = ['Ahmed', 'AlSayed', 'AbdulHameed', 'Osama', 'Gamal']

nameGenerator =  simpleGenerator( myList )

for name in myItemsList:
	print name

print myItemsList.next()
print myItemsList.next()
print myItemsList.next()
وده بس عشان البلوج كان عشش فيه العنكبوت ..

Written by me

June 27th, 2010 at 9:45 pm

Posted in I code,software

Tagged with ,

Survival..

without comments

السلام عليكم و رحمة الله وبركاته

Last 2 weeks

I was working last 2 weeks -between Cairo and Mansoura- for tadabborIT, for tadabbor website next release, also I’ve attended the JDC2010 conference and through that week I should have read about 4 reading tasks and should have solved tasks by Design patterns course by my dear mentor Ahmed Soliman, while planning to join GSoC.

Not to mention the courses and websites I’m delaying and canceling ..

And here I’m defining a new busyness level ..

One note before i go: If i survived that stage, I think I’ll be much more stronger than what I used to be, I also believe that It should be a milestone.

Anyway, I believe I can try :)

Written by me

March 13th, 2010 at 3:57 pm

Get Adobe Flash playerPlugin by wpburn.com wordpress themes