2016-11-27 42 views
7

Tôi có mã sau, nơi tôi gặp lỗi "Lỗi nghiêm trọng PHP: Biểu thức liên tục chứa hoạt động không hợp lệ". Nó hoạt động tốt khi tôi xác định các biến trong constructor. Tôi đang sử dụng khuôn khổ Laravel.Biểu thức liên tục chứa hoạt động không hợp lệ

<?php 

namespace App; 

class Amazon 
{ 
    protected $serviceURL = config('api.amazon.service_url'); 

    public function __construct() 
    { 
    } 

} 
+0

bạn không thể sử dụng các chức năng tại thời điểm đó, di chuyển nó đến các nhà xây dựng – RST

+0

bạn cần gán giá trị serviceURL bên trong hàm construct() –

Trả lời

11

Như đã trình bày here

Class member variables are called "properties". You may also see them referred to using other terms such as "attributes" or "fields", but for the purposes of this reference we will use "properties". They are defined by using one of the keywords public, protected, or private, followed by a normal variable declaration. This declaration may include an initialization, but this initialization must be a constant value--that is, it must be able to be evaluated at compile time and must not depend on run-time information in order to be evaluated.

Cách duy nhất bạn có thể làm cho công việc này là: -

<?php 

namespace App; 

class Amazon 
{ 
    protected $serviceURL; 

    public function __construct() 
    { 
    $this->serviceURL = config('api.amazon.service_url'); 
    } 
} 
0

Việc khởi tạo thuộc tính lớp không được phép theo cách này. Bạn phải di chuyển khởi tạo vào hàm tạo.

Các vấn đề liên quan