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


calling code from another php file (advanced)

我有两个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;
    }

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

如果您使用class来实现这一点,该怎么办?所以它变得像

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();

所以@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;
    }