Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/295.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在PHP中使用或不使用$this_Php_Oop_This - Fatal编程技术网

在PHP中使用或不使用$this

在PHP中使用或不使用$this,php,oop,this,Php,Oop,This,我是OOP PHP新手。为什么我们必须使用$this引用,而代码在没有它的情况下工作 如果我从这个代码片段中同时删除$this(before->sql),那么修改也是可行的。虽然我读过它,但我仍然不明白给定方法中的$this是什么 class blogs{ private $servername = "localhost"; private $username = "root"; private $password = ""; private $dbname =

我是OOP PHP新手。为什么我们必须使用
$this
引用,而代码在没有它的情况下工作

如果我从这个代码片段中同时删除
$this
(before->sql),那么修改也是可行的。虽然我读过它,但我仍然不明白给定方法中的
$this
是什么

class blogs{

    private $servername = "localhost";
    private $username = "root";
    private $password = "";
    private $dbname = "asd";
    private $conn = "";
    private $result = "";
    private $row = "";
    private $sql = "";

    public function cmd_update(){
        $this->sql = "UPDATE 
                blogs 
                SET
                `name` = '".$_GET['input_name']."',
                `date` = '".date('Y-m-d H:m:s')."',
                WHERE
                id = ".$_GET['input_modifying_id']."        
        ";

        if ($this->conn->query($this->sql) === TRUE) {
            echo "Successfully modified!";
        } else {
            echo "Modifying Unsuccessful!";
        }       
    }

$this
指的是当前对象——在这种情况下,
$this
博客
类的缩写

让我们以这个示例类为例:

class Foo
{
    protected $a;

    public function __construct($a)
    {
        $this->a = $a;
    }

    public function getA()
    {
        return $this->a;
    }
}
在这里,我只是将$a分配给传递给新Foo实例的任何对象,并创建一个公共可访问函数来返回$a

我们使用$this来表示我们当前所处的位置,
$this->a
是受保护的$a

对于开发人员来说,这是一个非常方便的特性,因为这意味着我们不必创建自己的方式来获取实例,而无需重新声明。这也意味着我们可以使用模板更容易地使用。例如

class Foo
{
    public function sayHi()
    {
        return 'hello world';
    }
}

因为公共功能很好。。public我们可以使用$this访问父类:

<?php
    $bar = new Bar();
    echo $bar->sayHi(); # will output hello world
我们可以有这样一个博客类:

class GenericController
{
    # pretend we've done some PDO set up here where we set new PDO to $this->pdo
    public function load($id)
    {
        $sql = 'SELECT * FROM `blogs` WHERE `id` = :id';
        $res = $this->pdo->prepare($sql);
        $res->execute(array(':id' => $id));

        return (object) $res->fetch(PDO::FETCH_ASSOC);
    }
}

class Blog extends GenericController
{
    protected $id;
    protected $data;

    public function __construct($id)
    {
        $this->id = $id;
    }

    protected function loadData()
    {
        $this->data = $this->load($this->id);
    }

    public function getTitle()
    {
        return $this->data->title;
    }

    public function getContent()
    {
        return $this->data->content;
    }

    public function getImg()
    {
        return $this->data->img
    }
}

$blogOne = new Blog(1);
$blogTwo = new Blog(2);

echo $blogOne->getTitle(); # will output hello world
echo $blogTwo->getTitle(); # will output what we did last summer
进一步阅读:

出于MySQL注入的原因(您的代码目前对上述注入开放):


对于OOP:


和文档本身的
$this
(可通过基本链接找到):

当从对象上下文中调用方法时,伪变量$this可用$这是对调用对象的引用(通常是该方法所属的对象,但如果该方法是从辅助对象的上下文静态调用的,则可能是另一个对象)。从PHP7.0.0开始,从不兼容的上下文静态调用非静态方法会导致$this在方法中未定义


$this
是当前调用对象的引用,也可以说是正在使用的对象。当您使用类的多个实例时,您的代码库对于不同的实例是可管理的。此外,可视性允许您将想要的内容保持为私有或公共。并且,
$此
将在对象生命周期内起作用,一旦您设置,您可以接受对象/类范围内的任何位置,而不限于函数。

$此
用于引用您所在的类。如果你做了
$this->foo='bar'
,您可以从其他方法访问
$this->foo
(如果设置为
public
,则可以从类外访问),而不带
$this
的变量将是局部变量,只能在该特定方法的范围内访问。因此,
$this->foo
$foo
是不同范围内的两个不同变量。
$this
是类的当前实例。例如,如果您有多个
Blog
实例,则每个实例都可以有自己的
$this->title
,而不会干扰其他
Blog
实例。通过删除
$this->sql
前面的
$this>,您将其转换为一个在类中无法访问的私有范围变量(在
cmd_update
之外)。通过指定
$this->sql
,您将其分配给成员变量。旁注:看到您似乎是一个新的编码员,您的代码将接受sql注入。最好使用一个准备好的语句。您不希望有一天您的数据库被破坏或删除。很高兴为此也包含了一个准备好的语句。@Fun是的,我想我会为未来的安全代码做准备(双关语;))哈哈哈嘿,好的;-)
title = hello world
content = hello world
img = /path/to/img.jpg

title = what we did last summer
content = played in the park
img = /path/to/another/img.jpg
class GenericController
{
    # pretend we've done some PDO set up here where we set new PDO to $this->pdo
    public function load($id)
    {
        $sql = 'SELECT * FROM `blogs` WHERE `id` = :id';
        $res = $this->pdo->prepare($sql);
        $res->execute(array(':id' => $id));

        return (object) $res->fetch(PDO::FETCH_ASSOC);
    }
}

class Blog extends GenericController
{
    protected $id;
    protected $data;

    public function __construct($id)
    {
        $this->id = $id;
    }

    protected function loadData()
    {
        $this->data = $this->load($this->id);
    }

    public function getTitle()
    {
        return $this->data->title;
    }

    public function getContent()
    {
        return $this->data->content;
    }

    public function getImg()
    {
        return $this->data->img
    }
}

$blogOne = new Blog(1);
$blogTwo = new Blog(2);

echo $blogOne->getTitle(); # will output hello world
echo $blogTwo->getTitle(); # will output what we did last summer