# $this-\>assert\* vs self::assert\*

**URL:** <https://discourse.laminas.dev/t/this-assert-vs-self-assert/448>\
**Category:** Contributors\
**Tags:** phpunit\
**Created:** [January 25, 2018, 5:10pm UTC](https://discourse.laminas.dev/t/this-assert-vs-self-assert/448 "2018-01-25T17:10:53Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![xtreamwayz](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/xtreamwayz/32/27_2.png) [@xtreamwayz](https://discourse.laminas.dev/u/xtreamwayz)\
**Post date:** [January 25, 2018, 5:10pm UTC](https://discourse.laminas.dev/t/this-assert-vs-self-assert/448/1 "2018-01-25T17:10:53Z")

</div>

> The assertion methods are declared static and can be invoked from any context using PHPUnit\Framework\Assert::assertTrue(), for instance, or using $this-\>assertTrue() or self::assertTrue(), for instance, in a class that extends PHPUnit\Framework\TestCase.
> 
> A common question, especially from developers new to PHPUnit, is whether using $this-\>assertTrue() or self::assertTrue(), for instance, is “the right way” to invoke an assertion. The short answer is: there is no right way. And there is no wrong way, either. It is a matter of personal preference.

I see self::assert\* more and more I even reverted some PR’s because of it to enforce consistency at least in the same package. Today I asked @mwop what to do in zendframework. This was his answer:

> Whenever I see `self::`, I just change those to `$this->` when merging.

So basically the PR’s are like a ping pong game. Some are changing it to `self::` and the next PR others are changing it to `$this->` and vice versa. `self::` is used [all over the place](https://github.com/search?p=1&q=org%3Azendframework+%22self%3A%3Aassert%22&type=Code&utf8=%E2%9C%93).

Without an official statement we can’t tell contributors what to use in their PR’s. And from experience I know a quote from a private slack group is not good enough to stop the discussion 🙂

---

<div class="post-metadata">

**Author:** ![michalbundyra](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/michalbundyra/32/185_2.png) [@michalbundyra](https://discourse.laminas.dev/u/michalbundyra)\
**Post date:** [January 25, 2018, 6:16pm UTC](https://discourse.laminas.dev/t/this-assert-vs-self-assert/448/2 "2018-01-25T18:16:24Z")

</div>

I’d like to say only that I was defending `$this->...` for a long while, but recently I’ve started using `self::` (as it is one character less, and easier to type :P)…

Definitely we should have consistency across all repositories, maybe even we should add it to coding-standard.

I would ping here @ocramius and @mwop because these two guys have different opinion (I guess :D). Thanks guys 🙂

Thanks @xtreamwayz for opening this topic ! 👍

---

<div class="post-metadata">

**Author:** ![ise](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/ise/32/274_2.png) [@ise](https://discourse.laminas.dev/u/ise)\
**Post date:** [January 25, 2018, 6:24pm UTC](https://discourse.laminas.dev/t/this-assert-vs-self-assert/448/3 "2018-01-25T18:24:48Z")

</div>

A question for consideration: If i were to use codeception in replace of PHPUnit, would there be any side effects from using static asserts?

I suspect not, but I had to throw in the question.

---

<div class="post-metadata">

**Author:** ![kynx](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/kynx/32/142_2.png) [@kynx](https://discourse.laminas.dev/u/kynx)\
**Post date:** [January 25, 2018, 6:47pm UTC](https://discourse.laminas.dev/t/this-assert-vs-self-assert/448/4 "2018-01-25T18:47:56Z")

</div>

Sebastian Bergmann says $this:

> <https://github.com/sebastianbergmann/phpunit/issues/1914>

Or use the global function wrappers:  
[https://phpunit.de/manual/current/en/appendixes.assertions.html#appendixes.assertions.static-vs-non-static-usage-of-assertion-methods](https://phpunit.de/manual/current/en/appendixes.assertions.html#appendixes.assertions.static-vs-non-static-usage-of-assertion-methods)

---

<div class="post-metadata">

**Author:** ![matthew](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/matthew/32/14_2.png) [@matthew](https://discourse.laminas.dev/u/matthew)\
**Post date:** [January 25, 2018, 6:54pm UTC](https://discourse.laminas.dev/t/this-assert-vs-self-assert/448/5 "2018-01-25T18:54:08Z")

</div>

> [@michalbundyra](#):
>
> I would ping here @ocramius and @mwop because these two guys have different opinion

We use `$this->` basically _everywhere_ within the ZF repos, so standardizing on that is the path of least resistance.

As noted, I think the `Assert::` syntax has use _within closures_ as well as _within traits_; everywhere else, though, we should use `$this->`.

---

<div class="post-metadata">

**Author:** ![ocramius](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/ocramius/32/23_2.png) [@ocramius](https://discourse.laminas.dev/u/ocramius)\
**Post date:** [January 25, 2018, 7:00pm UTC](https://discourse.laminas.dev/t/this-assert-vs-self-assert/448/6 "2018-01-25T19:00:30Z")

</div>

Yup, but the signature says `static` anyway. IIRC phpstan also clogs on  
this, but I’d need to check.

---

<div class="post-metadata">

**Author:** ![ocramius](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/ocramius/32/23_2.png) [@ocramius](https://discourse.laminas.dev/u/ocramius)\
**Post date:** [January 25, 2018, 5:23pm UTC](https://discourse.laminas.dev/t/this-assert-vs-self-assert/448/7 "2018-01-25T17:23:35Z")

</div>

Interesting: I do the opposite, changing `$this->` to `self::` while  
merging.

Remember that the method is static, so static code analysis tools will  
catch this as a violation at some point, and hopefully PHP will stop mixing  
the two at some point in the future.

---

<div class="post-metadata">

**Author:** ![matthew](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/matthew/32/14_2.png) [@matthew](https://discourse.laminas.dev/u/matthew)\
**Post date:** [January 25, 2018, 9:04pm UTC](https://discourse.laminas.dev/t/this-assert-vs-self-assert/448/8 "2018-01-25T21:04:53Z")

</div>

> [@ocramius](#):
>
> Remember that the method is static, so static code analysis tools will
> 
> catch this as a violation at some point, and hopefully PHP will stop mixing
> 
> the two at some point in the future.

Why would I run static analysis on my test classes? I’m not sure I see any benefit to that, to be honest…

---

<div class="post-metadata">

**Author:** ![ocramius](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/ocramius/32/23_2.png) [@ocramius](https://discourse.laminas.dev/u/ocramius)\
**Post date:** [January 26, 2018, 9:18am UTC](https://discourse.laminas.dev/t/this-assert-vs-self-assert/448/9 "2018-01-26T09:18:43Z")

</div>

I run static analysis on test classes all the time: it finds parameter  
mismatches, method signature issues, stub misuse and it prevents me from  
writing impossible scenario tests

Marco Pivetta

[http://twitter.com/Ocramius](http://twitter.com/Ocramius)

[http://ocramius.github.com/](http://ocramius.github.com/)

---

<div class="post-metadata">

**Author:** ![tux-rampage](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/tux-rampage/32/237_2.png) [@tux-rampage](https://discourse.laminas.dev/u/tux-rampage)\
**Post date:** [January 26, 2018, 12:30pm UTC](https://discourse.laminas.dev/t/this-assert-vs-self-assert/448/10 "2018-01-26T12:30:51Z")

</div>

> Remember that the method is static, so static code analysis tools will  
> catch this as a violation at some point

Really? Then I’d consider this an error in the static analysis tool. In PHP a static method can be called satically and non-statically, this is no logical error (since `$this` context does not apply to static methods, even if called non-static). Only calling non-static methods statically would be an error, since these require `$this` context. Sebastian states this: There ist nor right- or wrong-way to do it, just a matter of taste.

---

<div class="post-metadata">

**Author:** ![ocramius](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/ocramius/32/23_2.png) [@ocramius](https://discourse.laminas.dev/u/ocramius)\
**Post date:** [January 26, 2018, 1:53pm UTC](https://discourse.laminas.dev/t/this-assert-vs-self-assert/448/11 "2018-01-26T13:53:05Z")

</div>

It is a mismatch in expectations: one is to call a function that may access  
shared mutable global state, the other one is specific to the state of the  
given object.

From a reader perspective, `self::` is a strong indicator that the current  
object state will not be modified, which is quite important. (yes, statics  
that modify shared global state are bad!)

Marco Pivetta

[http://twitter.com/Ocramius](http://twitter.com/Ocramius)

[http://ocramius.github.com/](http://ocramius.github.com/)

---

<div class="post-metadata">

**Author:** ![ise](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/ise/32/274_2.png) [@ise](https://discourse.laminas.dev/u/ise)\
**Post date:** [January 27, 2018, 1:18am UTC](https://discourse.laminas.dev/t/this-assert-vs-self-assert/448/12 "2018-01-27T01:18:20Z")

</div>

> self:: is a strong indicator that the current object state will not be modified

Or read for that matter. It makes sense to call it statically, I may have to start using this myself.

---

<div class="post-metadata">

**Author:** ![PowerKiKi](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/powerkiki/32/48_2.png) [@PowerKiKi](https://discourse.laminas.dev/u/PowerKiKi)\
**Post date:** [January 28, 2018, 10:35am UTC](https://discourse.laminas.dev/t/this-assert-vs-self-assert/448/13 "2018-01-28T10:35:48Z")

</div>

I’m in the `self::` boat myself (recently converted). There is no point in adding to the confusion by calling a static method non-statically. Just call it what it is.

---

<div class="post-metadata">

**Author:** ![asgrim](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/asgrim/32/36_2.png) [@asgrim](https://discourse.laminas.dev/u/asgrim)\
**Post date:** [January 28, 2018, 3:01pm UTC](https://discourse.laminas.dev/t/this-assert-vs-self-assert/448/14 "2018-01-28T15:01:11Z")

</div>

Yeah, I also use `self::` consistently. It’s declared static, and that tells me plainly the calls should be static, basically.

---

<div class="post-metadata">

**Author:** ![tux-rampage](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/tux-rampage/32/237_2.png) [@tux-rampage](https://discourse.laminas.dev/u/tux-rampage)\
**Post date:** [January 29, 2018, 10:55am UTC](https://discourse.laminas.dev/t/this-assert-vs-self-assert/448/15 "2018-01-29T10:55:07Z")

</div>

Actually I don’t mind how this will come out (As stated above, in PHP calling a static method non-statically does not matter).

But I’d like to raise awareness that a switch from `$this->` to `self::` would either result in inconsistent test-code or all components may need to be rewritten to use `self::`. Do we have the resources for this or can we live with the mixture for a while (or forever if nobody finds the time to contribute for a component)? Another thing is when a test expects an axception: This is done with `$this->expectException()` which is kind of an assertion that is not static. Also please note that the offical PHPUnit documentation shows all TestCase examples with `$this->`, so we might have to expect contributions with `$this->` (e.g. [https://phpunit.de/manual/current/en/appendixes.assertions.html#appendixes.assertions.assertCount](https://phpunit.de/manual/current/en/appendixes.assertions.html#appendixes.assertions.assertCount)).

These are just my 2cents about this topic.

---

<div class="post-metadata">

**Author:** ![basz](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/basz/32/94_2.png) [@basz](https://discourse.laminas.dev/u/basz)\
**Post date:** [February 1, 2018, 6:45pm UTC](https://discourse.laminas.dev/t/this-assert-vs-self-assert/448/16 "2018-02-01T18:45:52Z")

</div>

Im all for ‘zelf::’. Actually went in and changed it until some maintainer started laughing really hard. So I shut up and thought nothing of it further.

Am glad others are worked up about this too…,

---

<div class="post-metadata">

**Author:** ![xtreamwayz](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/xtreamwayz/32/27_2.png) [@xtreamwayz](https://discourse.laminas.dev/u/xtreamwayz)\
**Post date:** [February 2, 2018, 9:07pm UTC](https://discourse.laminas.dev/t/this-assert-vs-self-assert/448/17 "2018-02-02T21:07:18Z")

</div>

> [@basz](#):
>
> Im all for ‘zelf::’.

zelf:: ??? Dutch? 😃

---

<div class="post-metadata">

**Author:** ![eldt](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/eldt/32/501_2.png) [@eldt](https://discourse.laminas.dev/u/eldt)\
**Post date:** [February 3, 2018, 7:13pm UTC](https://discourse.laminas.dev/t/this-assert-vs-self-assert/448/18 "2018-02-03T19:13:29Z")

</div>

For me `$this->something` is natural whereas `self::something` is yet another weirdo from these so-called developers of Zend.

---

<div class="post-metadata">

**Author:** ![xtreamwayz](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/xtreamwayz/32/27_2.png) [@xtreamwayz](https://discourse.laminas.dev/u/xtreamwayz)\
**Post date:** [February 4, 2018, 6:57am UTC](https://discourse.laminas.dev/t/this-assert-vs-self-assert/448/19 "2018-02-04T06:57:50Z")

</div>

> [@eldt](#):
>
> For me $this-\>something is natural whereas self::something is yet another weirdo from these so-called developers of Zend.

Let me fix that for you:

> For me $this-\>something is natural whereas self::something I would not feel comfortable with if it was introduced by contributors to Zend Framework.

---

<div class="post-metadata">

**Author:** ![eldt](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/eldt/32/501_2.png) [@eldt](https://discourse.laminas.dev/u/eldt)\
**Post date:** [February 4, 2018, 2:55pm UTC](https://discourse.laminas.dev/t/this-assert-vs-self-assert/448/20 "2018-02-04T14:55:45Z")

</div>

Thats basically the same, but wrote differently…

The point with `$this->sth` is that its been used ever since, whereas `self::sth` has been invented out-of-nowhere just because sth had to change and developers had no idea what to chenge…

In other words =\> its been extremely stupid idea to introduce `self::sth` and force its usage by (progressively) making `$this->sth` deprecated…

And thats only one example of things invented because they have to invent sth… there are **many** more throughout PHP…

Whole Zend…

What I really miss much is **pure PHP** meaning from pre-Zend times… it **was** extremely nice programming language…

[Next page](https://discourse.laminas.dev/t/this-assert-vs-self-assert/448.md?page=2)
