@Kar92

Почему всегда возвращает последнюю переменную?

всегда возвращает последнюю id

<?php

class Login
{

    protected $id;

    public function Login()
    {


        $msg = '';

        $firebase = new \Firebase\FirebaseLib(DEFAULT_URL, DEFAULT_TOKEN);
        $json = $firebase->get(DEFAULT_PATH . "/ARCusers/");


        $json_ary = json_decode($json, true);


        if (is_array($json_ary) || is_object($json_ary)) {
            foreach ($json_ary as $this->id => $val) {

                $getEmail = $firebase->get(DEFAULT_PATH . "/ARCusers/$this->id/email");
                $getPassword = $firebase->get(DEFAULT_PATH . "/ARCusers/$this->id/password");
                $activation = $firebase->get(DEFAULT_PATH . "ARCusers/$this->id/activation");
                if (isset($_POST['login']) && !empty($_POST['email'])
                    && !empty($_POST['password'])
                ) {

                    if ('"' . $_POST['email'] . '"' == $getEmail &&
                        '"' . $_POST['password'] . '"' == $getPassword
                    ) {
                        $_SESSION['valid'] = true;
                        $_SESSION['timeout'] = time();
                        $_SESSION['email'] = '"' . $_POST['email'] . '"';
                        if ($activation == 'true') {
                            echo "Logged in.";
                            header("Location: newwelcome.php");
//                            echo "$this->id";
                            return $this->id;

                        } else {
                            echo "verify your email";
                        }

                    } else {
                        $msg = 'Wrong email or password';

                    }
                }
            }

        }


    }
}

?>


<?php
include_once "new login.php";

class OpenPage extends Login
{
    private $getId;

    public function openpage()
    {
        $this->Login();
        $this->getId = $this->id;

        $firebase = new \Firebase\FirebaseLib(DEFAULT_URL, DEFAULT_TOKEN);
        echo $this->id;;

    }
}

$openpage = new OpenPage();

?>
  • Вопрос задан
  • 236 просмотров
Пригласить эксперта
Ответы на вопрос 1
webinar
@webinar Куратор тега PHP
Учим yii: https://youtu.be/-WRMlGHLgRg
Вы перебираете массив меняя $this->id в foreach ($json_ary as $this->id => $val) {
почему Вы решили, что получите что-то отличное от последнего ключа?
функция login возращает первый $this->id удовлтворяющий условиям, но сам класс Login всегда будет содержать в $this->id последнее значение.
Ответ написан
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы
YCLIENTS Москва
от 200 000 до 350 000 ₽
Ведисофт Екатеринбург
от 25 000 ₽
ИТЦ Аусферр Магнитогорск
от 100 000 до 160 000 ₽