从另一个php文件调用代码(高级)

时间:2015-03-28 04:59:17

标签: php

我有2个PHP文件,一个名为booking_functions.php,另一个名为discount.php

discount.php中,我设置3个变量,这些变量在多个页面中调用,具体取决于所调用的页面

booking_functions.php中,我需要获取discount.php中创建的变量并将它们传递给多维数组。

问题是设置数组的条件也设置$page_name,但我假设它不更新discount.php,因此永远不会触发并获取正确的函数元素。

所有内容都有PHP标记,用户会话已初始化。

//pages specific to an activity, there are over 30
     $page_name = "Hiking";
     include ("discount.php");

然后

// discount.php
    if($page_name == "Hiking"){
      $discount ="some var";
      $non_discount ="some var"; 
      $savings = $non_discount - $discount;
     }

    if($page_name == "kayaking"){
      $discount ="some var"; 
      $non_discount ="some var"; 
      $savings = $non_discount - $discount;
     }
     // etc. 
     // returns the discount value 
     function get_discount(){return $discount;}

然后

 //booking_functions.php

    include ("discount.php");
    // a html form button that adds a multidimensional session array 
    if(isset($_POST["sports_add"])){

    // checks to see what was selected 
    $drop_sport = $_POST['sports_add_dropdown'];

    // there are around 30 of these checks creating arrays 
    if($drop_sport=='hiking_d'){
    $page_name = "Hiking";
    $price = get_discount();
    // add array item
    $activity_array=array(0 =>array(
    'i_locked'=>false,
    'i_name' =>'Hiking',
    'i_people'=>1,
    'i_price'=>$price,
    'i_sport_activity'=> 'sport',
    'i_base_price'=>$price
    ));}
    $_SESSION["activity"][] = $activity_array;
    }

我攻击它的方式是在include("discount.php");内的每个函数中设置$page_name之后使用booking_functions.php,但我认为这可以更好地构建。

2 个答案:

答案 0 :(得分:0)

如果你使用课程怎么办? 所以它变得像

class Discount
{
    private $discount;
    private $non_discount;
    private $savings;
    public function __construct ( $type ) {
        switch($type)
        {
            case "Hiking":
                $this->discount = "some var";
                $this->non_discount = "some var";
                $this->savings = $this->non_discount - $this->discount;
                break;
            default:
                break;
        }
    }

    public function get_discount()
    {
        return $this->discount;
    }
}

对于预订功能,您可以使用此

include ("Discount.php");
$discount = new Discount();
$discount->get_discount();

答案 1 :(得分:0)

所以,@ghost是对的,只需要在PHP的范围声明中进行挖掘。

以下是现在可用的更新代码,原始代码没有机会测试Renato的代码,但我可能会回过头来看看他的答案如何提高代码的整体效率和最佳实践。

所有折扣变量都会变成一个以$page_name变量为参数的函数。它现在适用于所有30个页面以及需要调用Booking_function.php

的外部部分

我还重新考虑了代码,因此添加的多维项目是1个函数而不是30个。

//discount.php 

    function get_discount($page_name){
    global $non_discounted, $discounted, $savings;

    if($page_name =="Hiking"){
    $discounted= "some var";
    $non_discounted = "some var";
    $savings= $non_discounted - $discounted;
    }

    if($page_name =="Kayaking"){
    $discounted= "some var";
    $non_discounted = "some var";
    $savings= $non_discounted - $discounted;
    }

然后

//booking_function.php 

    include ("discount.php");

    // a html form button that adds a multidimensional session array 
    if(isset($_POST["sports_add"])){

    // checks to see what was selected 
    $drop_sport = $_POST['sports_add_dropdown'];

    // call the function from discount passing in the variable to check
    // then call whichever global variable was updated.  
    get_discount("Hiking");
    $price = $non_discounted;

    // add array item
    $activity_array=array(0 =>array(
    'i_locked'=>false,
    'i_name' => $drop_sport,
    'i_people'=>1,
    'i_price'=>$price,
    'i_sport_activity'=> 'sport',
    'i_base_price'=>$price
    ));
    $_SESSION["activity"][] = $activity_array;
    }