search

Home  >  Q&A  >  body text

$this in PHP class

class DBmodel{
    private $name;
    public __construct(){
    $this->name = $name;
    }
    public function delete($name){
        $this->name = $name;
    }
}

Does $this in __construct represent __construct itself? Does $this in delete represent delete itself? Or does $this represent the entire class?

A _ Q _i_A _ Q _i_1139 days ago864

reply all(5)I'll reply

  • hbxncjs

    hbxncjs2021-10-19 16:21:04

    $this represents the entire class

    reply
    0
  • yntdx

    yntdx2021-10-12 13:44:40

    This object must be allocated using the new operator (not new[], not placement new, not a local object, not a global object); after deleting this, you cannot access any members of the object Variables and virtual functions (delete this recycles data, which includes the object's data members and vtable, excluding function code); after deleting this, the this pointer can no longer be accessed. In other words, you can't check it, compare it to other pointers, compare it to NULL, print it, convert it, or anything else

    reply
    0
  • autoload

    autoload2021-10-11 09:31:02

    $this represents an object, and the environment where $this is located is inside the method inside the class, so the $this object is accessed inside the class

    reply
    0
  • Cancelreply