21
Июл
2021

PHP ругается за неправильную инициализацию. В чем проблема?

require_once __DIR__ .'\Components\Notification.php';
require_once __DIR__ .'\..\vendor\autoload.php';
require_once __DIR__ .'\Components\Mail.php';


class DB{
    /* @var PDO $db*/
    public static $db;
    /* @var PDO $dbsite*/
    public static $dbsite;

    public static function init($dbLink, $dbSiteLink){
        static::$db = $dbLink;
        static::$dbsite = $dbSiteLink;
    }
}

DB::init($db, $dbsite);
Mail::init($mailHost,$mailPort,$mailFrom,$mailFromName,$mailPassword);

Мой файл Mail.php

<?php
class Mail{
    private static $mailHost;
    private static $mailPort;
    private static $mailFrom;
    private static $mailFromName;
    private static $mailPassword;

    public static function init($mailHost, $mailPort, $mailFrom, $mailFromName, $mailPassword){
        static::$mailHost = $mailHost;
        static::$mailPort = $mailPort;
        static::$mailFrom = $mailFrom;
        static::$mailFromName = $mailFromName;
        static::$mailPassword = $mailPassword;
    }

    public static function sendMail($to,$subject,$body)
    {
        $mail = new PHPMailer\PHPMailer\PHPMailer(true);
        try {
            //Server settings
            $mail->SMTPOptions = array(
                'ssl' => array(
                    'verify_peer' => false,
                    'verify_peer_name' => false,
                    'allow_self_signed' => true
                )
            );
            $mail->CharSet = 'UTF-8';
            $mail->SMTPDebug = 0;
            $mail->isSMTP();                                            // Set mailer to use SMTP
            $mail->Host       = static::$mailHost;       // Specify main and backup SMTP servers
            $mail->SMTPAuth   = true;                                   // Enable SMTP authentication
            $mail->Username   = static::$mailFrom;      // SMTP username
            $mail->Password   = static::$mailPassword;   // SMTP password
            $mail->SMTPSecure = 'ssl';                                  // Enable TLS encryption, `ssl` also accepted
            $mail->Port       = static::$mailPort;       // TCP port to connect to
            $mail->Timeout = 5;

            //Recipients
            $mail->setFrom(static::$mailFrom,static::$mailFromName);
            $mail->addAddress($to);

            // Content
            $mail->isHTML(true);                                  // Set email format to HTML
            $mail->Subject = $subject;
            $mail->Body    = $body;

            return $mail->send();
            //return true;
        } catch (Exception $e) {
            return false;
        }
    }

}

Ошибка здесь DB::init($db, $dbsite);
Notice: Undefined variable: db in C:\xampp\htdocs\bit-home.ru\account\common_functions.php on line 54

Ошибка здесь Mail::init($mailHost,$mailPort,$mailFrom,$mailFromName,$mailPassword); Notice: Undefined variable: mailHost in Notice: Undefined variable: mailPort in Notice: Undefined variable: mailFrom in Notice: Undefined variable: mailFromName Notice: Undefined variable: mailPassword in Fatal error: Uncaught Error: Call to a member function prepare() on null in C:\xampp\htdocs\bit-home.ru\account\common_functions.php:59 Stack trace: #0 C:\xampp\htdocs\bit-home.ru\account\index.php(14): require_once() #1 {main} thrown in

Источник: https://ru.stackoverflow.com/questions/1307304/php-%D1%80%D1%83%D0%B3%D0%B0%D0%B5%D1%82%D1%81%D1%8F-%D0%B7%D0%B0-%D0%BD%D0%B5%D0%BF%D1%80%D0%B0%D0%B2%D0%B8%D0%BB%D1%8C%D0%BD%D1%83%D1%8E-%D0%B8%D0%BD%D0%B8%D1%86%D0%B8%D0%B0%D0%BB%D0%B8%D0%B7%D0%B0%D1%86%D0%B8%D1%8E-%D0%92-%D1%87%D0%B5%D0%BC-%D0%BF%D1%80%D0%BE%D0%B1%D0%BB%D0%B5%D0%BC%D0%B0

Тебе может это понравится...

Добавить комментарий