Using $this in POO PHP

Asked

Viewed 925 times

1

I don’t quite understand what $this "gets".

In this case we have a code like this, for example:

<?php
class Teste {
    public $testando;
    public function VamosTestar(){
       this->$testando = false; 
   }
}

In the case there, $this is "replacing" what? The test class? If I do so, it will be the same thing?

<?php
    class Teste {
        public $testando;
        public function VamosTestar(){
           Teste->testando = false; 
       }
    }

In this case, I don’t think so, you put an error in my Netbeans IDE. But what I would use if I didn’t want to use $this?

  • 1

    The correct is $this->testando = false;, o this refers to the class in this example.

  • Edited, lack of attention from me. But for example, if I don’t want to use "$this" I can use the class name? How?

  • For the record, this Teste->testando = false; does not work, must understand first what is an "instantiated class" and what is "a class" ;)

  • That I am in doubt, William, thank you for answering, I will be able to research.

  • 1

2 answers

3

Utilize $this to refer to the object itself. Another option would be self, but this makes reference to the class itself. Simply put, the $this->variavel is for non-static methods, the self::variavel is for static members.

An example of $this and self use.

class Foo {
    // Variável privada
    private $bar = 1;

    // Variável estática
    private static $fooBar = 2;

    function __construct() {
        // Imprime ambas variáveis
        echo $this->bar . ' ' . self::$fooBar;
    }

    // Caso tente imprimir $bar com self, receberá uma mensagem de erro
    // Caso tente imprimir $fooBar com this, receberá uma mensagem de erro
}

2

Basically the this refers to the class itself.

Understand it this way:

class Teste {
    public $testando;
    public function VamosTestar(){
       $this->testando = false; 
       //estaClasse->testando ou Teste->testando
   }
}

$this: "This class"

testando attribute with name testing.

To access functions of the class itself or attributes of the class itself we use the word reset $this, the $this can also be used to access functions and attributes inherited from another class as in the example below:

class Pai {
    public $nome;
}

class Filho extends Pai {
    public function foo(){
        return $this->nome;
    }
}

Utilizing:

$filho = new Filho();
$filho->foo();

the $this search for the attribute $nome from the first class extended to the current class.;

  • So, you’ll always have to use $this, in case. Just so I understand for good.

  • 1

    @Lucascarvalho, yes if you want to access functions or attributes of the class itself where is the of extended classes you need to use the $this, many programming languages use this concept with this

  • You can’t stop me from using it. Or I could use $name->name?

  • I’m a little confused by this yet, I have to study more :| But thanks for the answers!!!

  • What are you still doubting? if you want to include in the question you asked editing it that surely someone will help you.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.