首页 > 解决方案 > 获取数组中返回的另一个文件中的函数值

问题描述

我在一个文件中有一个函数,它通知数组中的值,但我需要在另一个文件中获取这些值。

我正在使用magento,需要从具有发出请求的客户端名称的函数中获取信息,我试图获取first_name,但它没有用。

文件一.php

public function getCustomerInfo($customer, $order)
    {

        $email = htmlentities($customer->getEmail());
        if ($email == "") {
            $email = $order['customer_email'];
        }

        $first_name = htmlentities($customer->getFirstname());
        if ($first_name == "") {
            $first_name = $order->getBillingAddress()->getFirstname();
        }

        $last_name = htmlentities($customer->getLastname());
        if ($last_name == "") {
            $last_name = $order->getBillingAddress()->getLastname();
        }

        return array('email' => $email, 'first_name' => $first_name, 'last_name' => $last_name);
    }

文件二.php

require(../Model/fileone.php

public function getName(){

            $name = $this->getCustomerInfo($customer, $order);
            $first_name = $name['first_name'];
            return $first_name;
    }

我需要显示 first_name 中的客户端名称,但未显示。

标签: functionmagento

解决方案


您有两个文件,一个具有您想从另一个调用的功能。

要使用getCustomerInfo()另一个文件中的函数,您需要导入包含该函数的类(使用useor require_once())。

如果您导入其他文件,则在函数调用之前不会使用this->该函数,因为该getCustomerInfo()函数与该函数不属于同一类getName()

由于在您提供的示例中,这些函数不是类的一部分,因此您应该能够require_once(fileOne.php);在 fileTwo.php 的顶部以及getCustomerInfo()该文件中的任何位置调用。

根据函数的声明,调用getCustomerInfo()它时需要有两个参数。


推荐阅读