如何在 php 中定义类的属性?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11034330/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 23:37:42  来源:igfitidea点击:

How Do I define properties for a class in php?

phpdatabaseclassfunction

提问by Dan O'Boyle

I've received Mixed responses on this depending what walk-through I read,

根据我阅读的演练,我收到了不同的回应,

I've defined a class with 2 functions.

我定义了一个具有 2 个函数的类。

I want both functions to have access to the DB credentials

我希望这两个函数都可以访问数据库凭据

Currently, this code does not work unless I Copy and paste the variables into each function.

目前,除非我将变量复制并粘贴到每个函数中,否则此代码不起作用。

What am I doing wrong here?

我在这里做错了什么?

<?php
class database  {
function connect()  {
var $username="my_username";
var $servername="localhost";
var $database="my_DB";
var $password="An_Awesome_Password";
var $con;
    $con = mysql_connect($servername,$username,$password);

    if (!$con)  {
    die('Could not connect: ' . mysql_error());
                }
                    }

function disconnect()   {
    $con = mysql_connect($servername,$username,$password);

    if (!$con)  {
    die('Could not connect: ' . mysql_error());
                }
    mysql_close($con);
                        }
            }
?>

回答by Ja?ck

This block:

这个块:

var $username="my_username";
var $servername="localhost";
var $database="my_DB";
var $password="An_Awesome_Password";
var $con;

Should come before the function(), not inside it; but still inside the classdefinition.

应该在 之前function(),而不是在里面;但仍在class定义内。

And it's good form to add an explicit visibility; private to start with:

添加显式可见性是一种很好的形式;私人开始:

class database  {
    private $username="my_username";
    private $servername="localhost";
    // etc. etc.

Then, the functions refer to them as:

然后,函数将它们称为:

$this->username;
$this->con;
etc.


Ideally you will want to have those credentials to be passed in by the constructor:

理想情况下,您希望构造函数传入这些凭据:

private $servername;
private $database;
private $username;
private $password;
private $con;

function __construct($host, $user, $password, $dbname)
{
    $this->servername = $host;
    $this->username = $user;
    $this->password = $password;
    $this->database = $dbname;
}


Even more ideally, learn about PDO

更理想的是,了解PDO

回答by Zagor23

To access object property you need to use $this->property_name

要访问对象属性,您需要使用 $this->property_name

 $this->con = mysql_connect($this->servername,$this->username,$this->password);

Class code would go like this:

类代码如下:

<?php
class database  {
    var $username="my_username";
    var $servername="localhost";
    var $database="my_DB";
    var $password="An_Awesome_Password";
    var $con;

    function connect()  {

        $this->con = mysql_connect($this->servername,$this->username,$this->password);

        if (!$this->con)  {
            die('Could not connect: ' . mysql_error());
        }
    }

    function disconnect()   {
        $this->con = mysql_connect($this->servername,$this->username,$this->password);

        if (!$this->con)  {
            die('Could not connect: ' . mysql_error());
        }
        mysql_close($this->con);
    }
}
?>

回答by Crontab

Code styling aside, you need to define your variables outsidethe class methods but still inside the class. Something like:

撇开代码样式不谈,您需要在类方法之外但仍在类内定义变量。就像是:

class database {
    var $username = "my_username";
    // etc.

    function connect() {
        // connect code
        // $this->username == "my_username"
    }
}

回答by Mihai Iorga

you have to use the variables outside the function and use the constructand destructfunctions, they will ensure you the connection will be open and closed properly

您必须使用函数外部的变量并使用构造销毁函数,它们将确保您正确打开和关闭连接

class database  {
    private $username = 'username';
    private $servername = "localhost";
    private $database = "my_DB";
    private $password = "An_Awesome_Password";
    private $conId;

   public function __construct(){
        $con = mysql_connect($this->servername, $this->username, $this->password);
        $this->conId = $con;
        //..........
    }


    public function __destruct(){
        mysql_close($this->conId);
    }
}

回答by curtisdf

It's good practice to assign class properties and methods their scope, which can be "public", "protected", or "private", instead of using "var". Also, class properties are assigned within the class but outside of any function (a.k.a. "method"). Here's your class refactored:

为类属性和方法分配它们的范围是一种很好的做法,可以是“public”、“protected”或“private”,而不是使用“var”。此外,类属性在类内分配,但在任何函数(也称为“方法”)之外。这是您重构的类:

class database  {
    private $username="my_username";
    private $servername="localhost";
    private $database="my_DB";
    private $password="An_Awesome_Password";
    private $con;

    public function connect()  {
        if (!$this->con) {
            $this->con = mysql_connect(
                    $this->servername, $this->username, $this->password);

            if (!$this->con)  {
                die('Could not connect: ' . mysql_error());
            }
        }
    }

    public function disconnect()   {
        if ($this->con)  {
            mysql_close($this->con);
        }
    }
}

回答by user1456386

Variables are only available to be called within the scope they are created in.

变量只能在创建它们的范围内调用。

If you create a variable in a function it can only be used in that function, but the higher you define it on the ladder the more it is available to.

如果您在函数中创建了一个变量,它只能在该函数中使用,但您在阶梯上定义的越高,它可用的越多。

Global Public Private Function

全局公私功能