Archive for the ‘design patterns’ tag
Singleton mySQL database connection using PHP
فيما يلي مثال على الـ 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();
في رعاية الله و أمنه
yield statement .. by example
ده تمرين بسيط على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()
Survival..
السلام عليكم و رحمة الله وبركاته
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