المساعدة في إنشاء وظيفة PHP قابلة لإعادة الاستخدام والتي سوف "حيوي" استدعاء وظائف أخرى

StackOverflow https://stackoverflow.com/questions/2063691

سؤال

وهذه هي المرة الأولى لي أنا أحاول أن أصنع شيئا بجدية مع ديسيبل العلائقية في الخلية ونوع من CMS التي تم إنشاؤها في PHP مع كود إغنيتر.
جئت إلى الجزء حيث لا بد لي من إدراج بعض البيانات في عدد قليل من كثير لكثير الجداول ذات الصلة.
في قانون بلدي كل شيء يعمل بشكل جيد (وأيضا، مع مجرد بضع دقائق من الاختبار)، ولكن انا بحاجة الى بعض المساعدة في إنشاء وظيفة قابلة لإعادة الاستخدام والتي سوف يساعدني كثيرا لجعل جميع العلاقات في بلدي الجداول ...

وحاولت أن تعليق كل شيء في قانون بلدي، لذلك كل التفسيرات هي في وجود ...

<?php
function add(){

 // This is what user posted in form.
 // There are two input fields:
 // name is always only one record
 // country can be a single record or array separated with " | " characters
 // I use CodeIgniter's $this->input->post instead of $_POST[]
 $name = $this->input->post('name');
 $countries = $this->input->post('country');

 // Inserting data to first table
 $data = array('firstName' => htmlentities($name)); // preparing array for inserting
 $insert_name = $this->db->insert('names', $data); // inserting with CodeIgniter's help
 $last_inserted_ID = $this->db->insert_id(); // getting last inserted ID

 // Inserting data to second table

 // Formatting of posted string of countries
 // Users can post strings similar to this:
 // "Austria"
 // "Austria |"
 // "Austria | "
 // "Austria | Australia"
 // "Austria | Australia |"
 // "Austria | Australia | "
 // and similar variations
 // What I need here is clear array with country names
 $separator = strpos($countries,"|"); // check for "|" character
 if ($separator === FALSE){ // if there is no "|" character in string
  $countries_array[] = $countries; // array is only one value (only one country)
 } else {
  $countries_array = explode(" | ", $countries); // explode my array
  if (end($countries_array) == ""){ // if last item in array is ""
   array_pop($countries_array); // eliminate last (empty) item
  }
 }

 // Now, this is the part I think I will use lots of times.
 // I would like to make this a separate function so I could use it in many places :)
 // I would pass to that function few values and I would use one of them
 // to call different functions in this same class.
 // I guess I should pass data ($countries_array) and function names I wish to call?????? This is problematic part for my brain :))
 // Check the comments below...
 for ($i = 0; $i < sizeof($countries_array); $i++){
  $insertIDS = array(); // this will be an array of IDs of all countries
  $tempdata = $this->get_countries($countries_array[$i]); // query which looks if there is a country with specific name
                // Right here, instead of calling $this->get_countries
                // I would like to call different functions, for example
                // $this->get_links($links_array[$i])
                // or $this->get_categories($categories_array[$i])
                // etc.
  if(sizeof($tempdata) != 0){ // so, if a record already exists
   foreach ($tempdata as $k => $v){
    $insertIDS[] = $k; // insert those IDs in our array
   }
  } else { // and if a record does not exist in db
   $this->add_country($countries_array[$i]); // add it as a new record...
               // This is also one of the places where I would call different functions
               // for example $this->add_link($links_array[$i])
               // or $this->add_categories($categories_array[$i])
               // etc.
   $insertIDS[] = $this->db->insert_id(); // ...get its ID and add it to array
  }

  // Finally, insert all IDs into junction table!
  foreach ($insertIDS as $idKey => $idValue){
   $this->add_names_countries($last_inserted_ID, $idValue); // Another place for calling different functions
                  // example $this->add_names_links($last_inserted_ID, $idValue)
                  // etc.
  }
 }

}
?>

حسنا، والنظر في هذا القانون الآن، وأرى أن أتمكن من وضع هذا تنسيق جزء أيضا في تلك الوظيفة، ولكن هذا ليس صحيحا لذلك من المهم كثيرا الآن ...

وشكرا جزيلا جدا على اي مساعدة مع هذا !!

هل كانت مفيدة؟

المحلول

والطريقة المفضلة للقيام بذلك هو استخدام الجدول بوابة البيانات . بدلا من

$this->db->insert('countries', $data);

وإنشاء فصول للكل جدول في قاعدة البيانات الخاصة بك. بتغليف كل جدول المنطق CRUD في الصف، منها مثلا.

class Countries
{
    $protected $_db;

    public function __construct($db)
    {
        $this->_db = $db;
    }

    public function save(array $countries)
    {
        $this->db->insert('countries', $countries);
    }

    // ... other methods
}

وبالإضافة إلى ذلك، أقترح استخدام المعاملات للحصول على هذا النوع من العمل لأن كل هذه الأشياء ينتمي معا، وكنت على الأرجح لا أريد أن أدخل أي بيانات، إذا فشل أحد الاستعلامات. أنا لا أعرف كيف يعالج CodeIgnitor المعاملات، ولكن في الأساس، عليك أن تفعل ذلك بهذه الطريقة ثم:

$this->db->startTransaction();          // like try/catch for databases
$countries = new Countries($this->db);
$countries->save($countryData);
$links = new Links($this->db);
$links->save($linkData);
// ...
if($this->db->commit() === false) {     // returns true when no errors occured
    $this->db->rollback();              // undos in case something went wrong
}

ولئن كان هذا لا يجيب على سؤالك كيفية استدعاء دالة حيوي (<لأ href = "http://de3.php.net/manual/en/function.call-user-func.php" يختلط = "نوفولو noreferrer "> call_user_func() يمكن أن تفعل ذلك)، يفعل ذلك مثل المقترحة أعلاه يجعل التعليمات البرمجية أكثر للصيانة بكثير.

وسؤالك هو غامض بعض الشيء كما أن إذا كنت ترغب في تشغيل جميع الوظائف في تسلسل أو تريد فقط لتبادل اعتمادا على ما تقدم للمستخدم. وبالنسبة للحالة الأولى، استخدم نهج الصفقة. وبالنسبة للحالة الثانية، وكنت ببساطة مثيل الدرجة المناسبة ودعوة حفظ الأسلوب.

نصائح أخرى

وليس تماما تأكد من الاحتياجات الخاصة بك، ولكن أعتقد أنك قد تكون بعد call_user_func:

function process($countries) {
// do stuff
}

$function_name = 'process';

call_user_func($function_name, $countries);

وبهذه الطريقة، يمكنك تعيين حيوي وظيفة على أساس، مثلا، لائحة الدول.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top