gpt4 book ai didi

php - 特质中的变量

转载 作者:行者123 更新时间:2023-12-02 19:49:11 25 4
gpt4 key购买 nike

我在 Laravel 中创建了一个特征。

在 myconstruct 中,我正在调用一个设置('value') - 这是由 qcod/laravel-app-settings 提供的包裹。

但是在我的方法中,当我引用 $this->token 或 $this->org_id 时,它返回 NULL

我知道这些值已设置,因为它们在配置中显示,并且也在数据库中正确设置。

我的 PHP 代码是:

<?php

namespace App\Traits;

use Illuminate\Support\Facades\Log;

trait PropertyBaseTrait
{

private $org_id;
private $token;
private $is_set;

public function __construct()
{
$this->org_id = setting('propertybase_org');
$this->token = setting('propertybase_token');
}

public function base_url($method)
{
return 'https://pb-integrations-api-staging.herokuapp.com/api/v1/'.$method.'/'.$this->org_id.'';
}

public function post_lead($data)
{
$lead_data = array(
'first_name' => '',
'last_name' => '',
'email' => '',
'phone1' => '',
'phone2' => '',
'phone3' => '',
'address' => '',
'city' => '',
'state' => '',
'zip_code' => '',
'country_name' => '',
'landing_page' => '',
'search_term' => '',
'referral_source' => ''
);

$object_type = 'lead';
$action_type = 'create';

dd($this->token);

$endpoint = $this->base_url('messages');

$this->post_data( $endpoint, $object_type, $action_type, json_encode($data));
}

最佳答案

问题是你的特质中有构造,也许在你使用此特质的类(class)中。可能的情况:

class MyClass {
use MyTraitWithConstructor;

public function __construct(){
...
}
}

在这种情况下,特征构造函数不起作用。

你能做什么?

您可以像这样重命名特征构造函数:

class MyClass {

use PropertyBaseTrait {
PropertyBaseTrait::__construct as private __prConstruct;
}

public function __construct(){
$this->__prConstruct();
...
}
}

关于php - 特质中的变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58589726/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com