V
V
Vyacheslav2017-09-28 07:45:33
Regular Expressions
Vyacheslav, 2017-09-28 07:45:33

Why does it swear at Using $this when not in object context?

Hi all!
I can’t understand what the error
is, there is a helper, code:

<?php

namespace common\helpers;

use Yii;

class LastActivity
{
  private $userid;
  
  public function init()
  {
    $this->userid = Yii::$app->user->isGuest ? false : Yii::$app->user->identity->id;
  }
  
  static public function get()
  {
    return $this->userid;
  }
}

I call the helper:
<?php
use common\helpers\LastActivity;
?>

<?= LastActivity::get(1); ?>

I get the error
Using $this when not in object context
MHvFL8OsQhqNcxPyf9qRcQ.png
What am I doing wrong? :)
Neighbor helper uses similar stuff but no errors
Thanks in advance for your reply :)

Answer the question

In order to leave comments, you need to log in

4 answer(s)
V
VicTHOR, 2019-06-17
@lexstile

^text\/\p{Cyrillic}(?:\S*?|\S*\s\S*)\p{Cyrillic}$
The third line does not end with Cyrillic, it ends with a number.
by the way, instead of \p{Cirillic} it's easier to write [а-яё] and put ui modifiers to ignore case. Just a shorter entry.
UPD: what the author ultimately needs after text/:
1) only Cyrillic
2) not the first, not the last character and no more than 1 time can come across: space, apostrophe, dash.
I did not find another option, but I made a decision.

R
Rsa97, 2017-09-28
@nskarl

And what should $this be equal to when calling a static class method? An instance is not created with such a call.

S
spike__x, 2017-09-28
@spike__x

static public function get()
  {
    return self::userid;
  }

method is static

K
Konstantin Malyarov, 2017-09-28
@Konstantin18ko

It considers $this to be an object.
What's in $this?
var_dump() is possible?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question