php Laravel:从公共静态函数访问类变量(基本 oop 问题)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24062385/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel: Get access to class-variable from public static function (basic oop issues)
提问by Kristo
EDIT
编辑
In the meantime this question is been visited a few times. Just to share what I′ve learned with the help of stackoverflow and other resources I′d not recommend using the technique I was asking for. A cleaner way is to attach a variable containing the database-text within the controller:
与此同时,这个问题被访问了几次。只是为了分享我在 stackoverflow 和其他资源的帮助下学到的东西,我不建议使用我要求的技术。更简洁的方法是在控制器中附加一个包含数据库文本的变量:
$txt = Model::find(1);
return view('name', array('text' => $txt->content);
Now you can access text in your view like so
现在您可以像这样访问视图中的文本
{{ $text ?? 'Default' }}
But if you′re currently also hustling with basic oop and/or mvc architecture read on. Maybe it helps :-)
但是,如果您目前也在忙于使用基本的 oop 和/或 mvc 架构,请继续阅读。也许它有帮助:-)
ORIGINAL QUESTION
原问题
I′m trying to output some text loaded from db. This is my setup:
我正在尝试输出一些从 db 加载的文本。这是我的设置:
View:
看法:
{{ ContentController::getContent('starttext') }}
Controller:
控制器:
class ContentController extends BaseController {
public $text = '';
public static function getContent($description)
{
$content = Content::where('description', $description)->get();
$this->text = $content[0]->text;
return $this->text;
}
}
I was trying various ways to declare a class variable and access it in my function but I always end up with:
我尝试了各种方法来声明一个类变量并在我的函数中访问它,但我总是得到:
Using $this when not in object context
不在对象上下文中时使用 $this
tbh I think I lack some basic oop knowledge :-D
tbh 我想我缺乏一些基本的 oop 知识:-D
采纳答案by Populus
Static methods do not have access to $this
. $this
refers to an instantiated class (an object created with a new
statement, e.g. $obj = new ContentController()
), and static methods are not executed within an object.
静态方法无权访问$this
. $this
指的是一个实例化的类(一个用new
语句创建的对象,例如$obj = new ContentController()
),并且静态方法不在对象内执行。
What you need to do is change all the $this
to self
, e.g. self::$text
to access a staticvariable defined in your class. Then you need to change public $text = '';
to public static $text = '';
您需要做的是将所有内容更改$this
为self
,例如self::$text
访问类中定义的静态变量。然后你需要更改public $text = '';
为public static $text = '';
This is why static methods/variables are bad practices most of the time...
这就是为什么静态方法/变量在大多数情况下都是不好的做法......
Not an expert at Laravel, but I'm sure you don't need to use static methods to pass variables into templates... If that is the case, I'm staying the hell away from Laravel...
不是 Laravel 的专家,但我确定您不需要使用静态方法将变量传递到模板中...如果是这种情况,我将远离 Laravel...
回答by The Alpha
You may try something like this (In the case of static
):
你可以尝试这样的事情(在 的情况下static
):
class ContentController extends BaseController {
public static $text = null;
public static function getContent($description)
{
$content = Content::where('description', $description)->first();
return static::$text = $content->text;
}
}
Read the other answer to understand the difference; also read about Late Static Bindingsbut instead...
阅读其他答案以了解差异;还阅读了Late Static Bindings但相反......
You may try something like this in Laravel
to avoid static
:
您可以尝试这样的方法Laravel
来避免static
:
class ContentController extends BaseController {
public $text = null;
public function getContent($description)
{
$content = Content::where('description', $description)->first();
return $this->text = $content->text;
}
}
Use it like this:
像这样使用它:
{{ App::make('ContentController')->getContent('starttext') }}
Also this:
还有这个:
{{ with(new ContentController)->getContent('starttext') }}
Or this (Even without Laravel
):
或者这个(即使没有Laravel
):
{{ (new ContentController)->getContent('starttext') }}
回答by Chris L
In Laravel 5.7 the or
operator has been removed so {{ $text or 'Default' }}
doesn't work anymore.
New operator is ??
. Beginning with Laravel 5.7 it should be {{ $text ?? 'Default' }}
在 Laravel 5.7 中,or
运算符已被删除,因此{{ $text or 'Default' }}
不再起作用。新运算符是??
。从 Laravel 5.7 开始,它应该是{{ $text ?? 'Default' }}