简体   繁体   中英

Use variable from “public static function” in another “public static function”

I know this question is asked like a hundred times in hundred different ways. But I just can't get this particular one to work. I am really confused.

This is the very beginning of my script:

<?php

class API {

    protected static $message, $data, $status, $session, $token, $dbo, $app;

    const FAIL = 0;
    const SUCCESS = 1;
    const INCOMPLETE_ARGS = 2;
    const GROUP_ALLOW = array(5);

    public static function Init() {
        global $app;
        self::$status = false;
        self::$message = "Invalid request.";
        self::$data = "";
    }

    public static function Render() {
        echo preg_replace("/\\\\u([0-9abcdef]{4})/", "&#x$1;", json_encode(array(
            "status" => self::$status,
            "message" => self::$message,
            "data" => self::$data))
        );
    }

Then there is this function:

      public static function Login() {
      $email = (isset($_REQUEST['email'])) ? $_REQUEST['email'] : "";
                $password = (isset($_REQUEST['password'])) ? $_REQUEST['password'] : "";

                require_once ("../app/Mage.php");
                umask(0);
                ob_start();
                session_start();
                Mage::app('default');
                Mage::getSingleton("core/session", array("name" => "frontend"));
                $b2bwebsiteid = Mage::getStoreConfig('base/general/b2bwebsite');
                //$websiteId            = Mage::app()->getWebsite()->getId();
                //$store                = Mage::app()->getStore();

                $customer = Mage::getModel("customer/customer");
                $customer->website_id = $b2bwebsiteid;
                $customer->loadByEmail($email);
                $countryCodeBE = $customer->getDefaultBillingAddress()->getCountry();
                $countrybe = Mage::getModel('directory/country')->loadByCode($countryCodeBE);
                $countryid = $countrybe->getCountryId();
    .....
    .....

And in between there is a lot more code and functions.

Now a bit further, there is another function:

public static function ProductInfoNew() {

And now the thing I want, is to load the variable $countryid from that first function in the ProductInfoNew function so that I can do something like this:

if ($countryid == "BE") {
                $groupid = 6;
            }
            else {
                $groupid = 4;
            }

But the output always seems to be NULL or Empty.

I tried setting the $countryid variable as global, static, tried to load the variable with self:: and I tried so many things but I can't get it to work.

Does anyone know the correct solution to do this? Thanks a lot in advance.

$countryid should be a field inside of the class you are using.

class API {

    private static $country_id;

    public static function ProductInfoNew() {
        if (self::$country_id == "BE") {
            $groupid = 6;
        } else {
            $groupid = 4;
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM