Динамические переменные класса

Вот адаптация Swift 3 ответа Кабреры :

let bundle = Bundle.main
        let path = bundle.path(forResource: "GoogleService-Info", ofType: "plist")!
        let options = FIROptions.init(contentsOfFile: path)
        FIRApp.configure(withName: "Secondary", options: options!)
        let secondary_app = FIRApp.init(named: "Secondary")
        let second_auth = FIRAuth(app : secondary_app!)
        second_auth?.createUser(withEmail: self.username.text!, password: self.password.text!)
        {
            (user,error) in
            print(user!.email!)
            print(FIRAuth.auth()?.currentUser?.email ?? "default")
        }

6
задан hakre 1 July 2012 в 15:06
поделиться

2 ответа

Посмотрите здесь: http://www.php.net/manual/en/language.oop5.overloading.php

То, что Вы хотите, "__, получают" метод. Существует пример для того, в чем Вы нуждаетесь на ссылке.

12
ответ дан 8 December 2019 в 12:24
поделиться

Используйте "волшебство" PHP5 __get() метод. Это работало бы как так:

public class TestClass {
    private $data = array();

    // Since you're using PHP5, you should be using PHP5 style constructors.
    public function __construct() {
        $this->data['firstValue'] = "cheese";
    }

    /**
     * This is the magic get function.  Any class variable you try to access from 
     * outside the class that is not public will go through this method.  The variable
     * name will be passed in to the $param parameter.  For this example, all 
     * will be retrieved from the private $data array.  If the variable doesn't exist
     * in the array, then the method will return null.
     *
     * @param string $param Class variable name
     *
     * @return mixed
     */
    public function __get($param) {
        if (isset($this->data[$param])) {
            return $this->data[$param];
        } else {
            return null;
        }
    }

    /**
     * This is the "magic" isset method.  It is very important to implement this 
     * method when using __get to change or retrieve data members from private or 
     * protected members.  If it is not implemented, code that checks to see if a
     * particular variable has been set will fail even though you'll be able to 
     * retrieve a value for that variable.
     *
     * @param string $param Variable name to check
     * 
     * @return boolean
     */
    public function __isset($param) {
        return isset($this->data[$param]);
    }

    /**
     * This method is required if you want to be able to set variables from outside
     * your class without providing explicit setter options.  Similar to accessing
     * a variable using $foo = $object->firstValue, this method allows you to set 
     * the value of a variable (any variable in this case, but it can be limited 
     * by modifying this method) by doing something like:
     * $this->secondValue = 'foo';
     * 
     * @param string $param Class variable name to set
     * @param mixed  $value Value to set
     * 
     * @return null
     */
    public function __set($param, $value) {
        $this->data[$param] = $value;
    }
}

Используя волшебство __get, __set, и __isset конструкторы позволят Вам управлять, как Вы хотите, чтобы переменные были установлены на классе, все еще храня все значения в едином массиве.

Надежда это помогает :)

7
ответ дан 8 December 2019 в 12:24
поделиться
Другие вопросы по тегам:

Похожие вопросы: