الأربعاء، 10 أبريل 2013

Class overloadable to make ability for overloading in php كلاس لتمكين التحميل الزائد في php

بعد طول انقطاع أعود للتدوين.. مرحبا بكم :) أبدأ على بركة الله تدوينة جديدة: تحدثت في مقالة سابقة Overloading in php just like C Sharp عن كيفية عمل overload للغة php تشبه الى حد ما خاصية overload في c// الآن اقدم طريقة أحدث، وكود الكلاس كما يلي:

class overloadable 
{ 
 protected $args = array();
 private $argType = array();
 private $argArry = array();
 private $constructArgsAry = array();
 private $method;
 private $className ='';
 
 function setConstructArgs($args) {
  $this->constructArgsAry = $args;
 }
 //--------------------------------------------------//
 function getMethod($className, $method, $args) {
  $this->className = $className;
  
  $this->args = $args;
  $type = $this->getTypes($args);
  
  $method = $method.'_'.$type;
  $this->method = $method; 
  
  return $this->callFunc();
 }
 //--------------------------------------------------//
 function getTypes($args) {
  foreach($args as $i=>$val) {
   $this->argType[$i] = $this->getType($val) ;
  }
  return implode('', $this->argType);
 }
 //--------------------------------------------------//
 function getType($byValarg) {
   if( is_numeric($byValarg) ) {
    $type = 'N'; //'numeric';
   } elseif( is_array($byValarg) ) {
    $type = 'A'; //'array';
   } elseif( is_object($byValarg) ) {
    $type = 'O'; //'object';
   } elseif( is_resource($byValarg) ) {
    $type = 'R'; //'resource';
   } else {
    $type = 'S'; //'string';
   } 
   return $type;
 }
 //--------------------------------------------------//
 function callFunc() {
  $className = $this->className;
  $method = $this->method;
  
  $constr_argsStringKeys='';

  $num_args = count($this->args);
  $argsAry = array();
  if($num_args>0){
   
   for($i=0; $i<$num_args; $i++) {
    $argsAry['$arg_'.$i] = $this->args[$i];
    eval("\$arg_$i = \$this->args[$i];");
   }
   $argsStringKeys = implode(', ', array_keys($argsAry));

   $func = create_function($argsStringKeys, 
      '$d = new '.$className.'('
      . $constr_argsStringKeys .'); return $d->'
      . $method.'('.$argsStringKeys.');');
   
   eval("\$f = \$func($argsStringKeys);"); 
   return $f;
  } 
 }
}

طريقة الاستخدام : يجب أن تقوم بعمل وراثة من الكلاس overloadable يجب اضافة كود معين للدالة التي لا تحتوي على أي براميتر. ثم اضافة دوال أخرى بنفس الاسم ولكن يضاف في نهاية اسم الدالة شرطة مائلة _ ثم أول حرف من نوع المتغير فمثلا: الدالة hi_s تعني أن هذه الدالة لها براميتر واحد نصي الدالة hi_sa تعني ان هذه الدالة لها براميترين واحد نصي والثاني مصفوفة. مثلاً يتم بناء كلاس وليكن اسمه myClass:

class myClass extends overloadable 
{
 function hi() {
  // important to work other methods
  $r = parent::getMethod(__CLASS__, __FUNCTION__, func_get_args());
  if($r){ return $r; }
  // code for method hi();
  return 'hi!';
 }
 function hi_s($name){
  return "Hi $name";
 }
 function hi_ss($var1, $var2){
  return "Hi $var1 $var2";
 }
 function hi_sa($name, $ary){
  $r = $name.' have ';
  foreach($ary as $thing){
   $r .= "$thing and ";
  }
  return rtrim($r, 'and ') .'.';
 }
 
 function hi_aa($ary1, $ary2){
  return 'Ary1: '. print_r($ary1, 1).' < br />ARY2:'. print_r($ary2, 1);
 }
 
 function wins(){
  $r = parent::getMethod(__CLASS__, __FUNCTION__, func_get_args());
  if($r){ return $r; }
  return 'You are the winner!';
 }
 
 function wins_sn($wining, $cash){
  return $wining.' wins '. $cash .'$';
 }
}

مثال على استخدام الكلاس myClass:

$o = new myClass();

echo '<pre>';
print_r( $o->hi() );
echo '< br />';
print_r( $o->hi('saleem', array('a job', 'a car', 'a house') ) );
echo '< br />';
print_r( $o->hi('Hishahm', 'Dalal') );
echo '< br />';
print_r( $o->hi(array(1,2), array('a','b') ) );
echo '< br />';
print_r( $o->wins('Hisahm', 1000) );
echo '< br />';
print_r( $o->wins() );

لتحميل المثال كاملا انقر هنا

الأحد، 27 نوفمبر 2011

Overloading in php just like C Sharp

كيف تحصل على التحميل الزائد بشكل أكثر شبها بلغة c#
ففي لغة C# يمكننا عمل ما يلي:
// author: hishamdalal@gmail.com 2011
class person
{
    private int ID;
    private string Name;

    public void Add(string ID, string Name)
    {
        this.ID = Convert.ToInt32(ID);
        this.Name = Name;
    }
    public person()
    {
        this.Add("0", "no name!");
    }
    public person(string ID, string Name)
    {
        this.Add(ID, Name);
    }
}

students s = new students();
s.person();             // method without args
s.person(1, "hisham");  // overload method with args

فكيف يمكننا فعل ذلك بواسطة php?

الثلاثاء، 22 نوفمبر 2011

ضع خريطة في مدونتك

Google LatLong: Power to the print:


Google Maps can help get you where you want to go, but let's face it, you can't be online all the time. Sometimes you really just need a goo...

الأربعاء، 3 أغسطس 2011

كلاس للقراءة من والكتابة في ملف مضغوط من نوع gzip




كلاس للقراءة من والكتابة في ملف مضغوط من نوع gzip

طريقة العمل:


إنشاء كائن gzip وتحديد مكان الملف المضغوط

إذا لم يكن الملف موجود سيتم انشاءه في حالة الكتابة



// Creat object and select gzip file:
$gz = new gzip('tmp/somefile.txt.gz');




الكتابة الى الملف المضغوط وتحديد مستوى الضغط

من صفر الى تسعة



// Write to gzip file and select compress level:
echo $gz->write("hello every body,\nI'm in a gzip file..\nthats fun! ", 9);




القراءة من الملف المضغوط

الطريقة الافتراضية readFile() تعيد مصفوفة بالسطور

وبدون فك للضغط



// Read form gzip file as array:
#echo '
'; print_r( $gz->readFile() ); echo '
';




قراءة الملف كاملاً بشكل نص وليس مصفوفة



// Read from gzip file as string:
#$gz->readFile('all');




القراءة من ملف مضغوط واختيار أن يتم فك الضغط



// Read from gzip file as string and select to uncompress:
echo $gz->readFile('all', true);



قراءة عدد معين من الحروف واختيار أن يتم فك الضغط



// Read 'x' chars from gzip file as string and select to uncompress:
#echo $gz->readData(17, true);



ملاحظة:

في حالة قمت بضغط الملف فيجب عليك اختيار فك الضغط

وإلا سيتم عرض نص مشفر غير مفهوم



يعمل هذا الكلاس على الاصدارة : PHP Version 6.0.0-dev بدون مشاكل
للاصدارات الأقدم استخدم:


echo ( $gz->readFile('eof', true) ); 



الكلاس:


/************
* Author : hisham dalal 
* Date   : 2011-07-15
*************/

class gzip{

    private $filename;
    private $link;

    function __construct($filename){
      $this->setFile($filename);
    }
    private function setFile($filename){
        $this->filename = $filename;
    }
    private function open($type='r'){
        $this->link = gzopen($this->filename, $type); //w9
        return $zp;
    }

    function write($data, $compressLevel=-1){
        if($compressLevel>-1){
          $data = gzcompress($data, $compressLevel);
        }
        $this->open('w9');
        return gzputs ($this->link, $data);
    }
    function readData($length, $uncompress=false){
      $this->open('r');
        // read $length char
        $data = gzread($this->link, $length);   #echo $s;
        if($uncompress){
          $data = gzuncompress($data);
        }
        return $data;
    }
    function readFile($type='array', $uncompress=false){

        $this->open('r');
        // output until end of the file.
        ob_start();
        switch($type){
          case 'array': echo gzfile($this->filename); break;
          case 'all'  : gzpassthru($this->link);      break;
          case 'eof'  : readgzfile($this->filename);  break;
        }
        $r = ob_get_contents();
        ob_clean();

        if($uncompress){
          $data = gzuncompress($r);
          return $data;
        }
        return $r;
    }
    function __destruct(){
        gzclose($this->link);
    }
}


السبت، 16 أبريل 2011

dynamicdrive.com

موقع dynamicdrive.com

يختص بتقديم الإضافات الجمالية للموقع والتي تضيف اليها الحياة، وهذه بعض التصنيفات:

الثلاثاء، 12 أبريل 2011

الكتاب الذي تعلمت من خلاله لغة php

طبعاً الكتاب المترجم من دار الشعاع للنشر.. لكنه لم يعد موجوداً في موقعهم..
لذا بحثت عن اسم الكتاب بالانجليزي "موجود اسم الكتاب الانجليزي" في الطبعة العربية
صورة الكتاب:
http://www.amazon.com/gp/reader/1931841322/ref=sib_dp_pt#reader-link

والرابط لمن يريده كالتالي:
http://www.mediafire.com/?baoff9gibnia66x

تنظيم واستيراد الكلاسات من داخل الكلاسات:

يمكنك جعل الكلاس يدرج كلاس آخر ويصل الى كل طرقه،
وبالتالي سيمكنك ذلك من تنظيم كلاساتك..

طريقة العمل:

لنفترض أن لدينا الكلاس a على النحو التالي:

class a {
    function m1(){
        return "a [m1]";
    }
    function m2($x){
        return "a [m2][$x]";
    }
}



والكلاس b على النحو التالي:


class b {
    private $obj_name;
    private $obj;    
    
    function load($obj){
        if(class_exists($obj)){
            $this->obj_name = $obj;
            $this->obj = new $obj();           
        }
    }
    function get_method($method, $var=''){
        return $this->obj->$method($var);
    }
    function get(){
        $this->a = new a();
        echo $this->a->m1();
    }
    function get_obj(){
     return $this->obj;
    }
}



فإن لدينا عدة طرق للحصول على طرق الكائن a من خلال الكائن b:


$b = new b();
$b->load('a');
echo $b->get_method('m1');
echo '<br>';
echo $b->get_method('m2', 100);
echo '<br>';
$b->get();
echo '<br>';
echo $b->get_obj()->m2(200);


شرح الكلاس b:

1) المتغيرات:


private $obj_name;
    private $obj; 

- المتغير $obj_name يحفظ اسم الكلاس الذي سيتم استيراده
- المتغير $obj يحفظ الكائن (بعد التنفيذ)

شرح دوال الكلاس b:

الدالة load:

function load($obj){
        if(class_exists($obj)){
            $this->obj_name = $obj;
            $this->obj = new $obj();           
        }
    }


تتحقق من وجود الكلاس أولاً
تحتفظ باسم الكلاس في المتغير obj_name
تحتفظ بنسخة للكائن في المتغير obj



الدالة get_method:


function get_method($method, $var=''){
        return $this->obj->$method($var);
    }

تعيد طريقة من طرق الكائن المستورد مع امكانية تمرير وسيط


الدالة get

function get(){
        $this->a = new a();
        echo $this->a->m1();
    }



طريقة مباشرة للحصول على إحدى طرق الكائن المستورد


الدالة get_obj

function get_obj(){
     return $this->obj;
    }


طريقة للحصول على الكائن بكل طرقه

 
Design by Free WordPress Themes | Bloggerized by Lasantha - Premium Blogger Themes | Blogger Templates | تعريب وتطوير : قوالب بلوجر معربة