我有点困惑,因为我在PHP中没有太多的OOP经验.我总是听说使用实例方法比使用静态方法更好.为什么?
我需要一个深刻的答案和解释.
例如,为什么下面这个应该用实例方法完成?
控制器:
public function getProperty($id){
$property = Property::getProperty($id);
return $property;
}
Run Code Online (Sandbox Code Playgroud)
模型:
public static function getProperty($id){
//$query = DB::table('properties')...
//Some Code;
return $query;
}
Run Code Online (Sandbox Code Playgroud)
我正在阅读有关setter/getter,instance vs static等的内容.但我需要一个完整的答案来理解事物的方式和原因.
我已从我的文件启用OpenSSL并IMAP运行php.ini并phpinfo()确认它.
通过使用下面的代码,我可以连接到Hotmail帐户,但不能连接到Gmail帐户.(当然,我将更$connect_to改为指向Hotmail.)
$connect_to = '{imap.gmail.com:993/imap/ssl}INBOX';
$connection = imap_open($connect_to, $user, $password)
or die("Can't connect to '$connect_to': " . imap_last_error());
imap_close($connection);
Run Code Online (Sandbox Code Playgroud)
返回的错误是
Warning: imap_open(): Couldn't open stream {imap.gmail.com:993/imap/ssl}INBOX in /opt/lampp/htdocs/webmail_client_practise/index.php on line 6
Can't connect to '{imap.gmail.com:993/imap/ssl}INBOX': Certificate failure for imap.gmail.com: unable to get local issuer certificate: /C=US/O=GeoTrust Inc./CN=GeoTrust Global CA
Notice: Unknown: Certificate failure for imap.gmail.com: unable to get local issuer certificate: /C=US/O=GeoTrust Inc./CN=GeoTrust Global CA (errflg=2) in Unknown on line …Run Code Online (Sandbox Code Playgroud) 基于PHPMailer提供的示例,我有以下脚本,
date_default_timezone_set('Etc/UTC');
require './PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->isSMTP();
$mail->SMTPDebug = 2;
$mail->Debugoutput = 'html';
$mail->Host = 'smtp.gmail.com';
$mail->Port = 587;
$mail->SMTPSecure = 'tls';
$mail->SMTPAuth = true;
$mail->Username = "myemail@example.com";
$mail->Password = "********";
$mail->setFrom('myMail@example.com', 'First Last');
$mail->addReplyTo('myEmail@example.com', 'First Last');
$mail->addAddress('toEmail@example.com', 'first last');
$mail->Subject = 'PHPMailer GMail SMTP test';
$mail->Body = "example";
$mail->AltBody = 'This is a plain-text message body';
if (!$mail->send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
Run Code Online (Sandbox Code Playgroud)
即使这与原始示例完全相同,我也无法使其工作.
我得到的错误是
警告:stream_socket_enable_crypto():SSL操作失败,代码为1. OpenSSL错误消息:错误:14090086:SSL例程:SSL3_GET_SERVER_CERTIFICATE:证书验证在第344行的/opt/lampp/htdocs/webmail_client_practise/class.smtp.php中失败SMTP错误:无法连接到SMTP主机. …
我做了什么:
我创建了这个自定义控制器,因为我想向错误页面传递一些额外的变量。
#Controller/CustomErrorControler.php
namespace App\Controller;
use App\Controller\Base\BaseController;
use Symfony\Component\ErrorHandler\Exception\FlattenException;
use Symfony\Component\HttpKernel\Log\DebugLoggerInterface;
class CustomErrorController extends BaseController
{
public function show(FlattenException $exception, DebugLoggerInterface $logger = null)
{
return $this->getView('bundles/TwigBundle/Exception/error.html.twig', [
"code" => $exception->getStatusCode(),
"message" =>$exception->getStatusText()
]);
}
}
Run Code Online (Sandbox Code Playgroud)
和启用的
#config/packages/framework.yaml
error_controller: App\Controller\CustomErrorController::show
Run Code Online (Sandbox Code Playgroud)
我直接按照文档进行操作。我的问题是,对于非生产阶段,我需要获取框架提供的默认异常模板。
我尝试过扩展Symfony\Component\HttpKernel\Controller\ErrorController,但出现自动装配错误。
也许我应该使用Symfony\Component\ErrorHandler\ErrorRenderer\ErrorRendererInterface
任何想法如何实现这个?