简体   繁体   English

PHP OOP-我不了解属性的好处是什么?

[英]PHP OOP - I don't understand what is benefit of properties?

the thing I always use in OOP is method . 我一直在OOP中使用的是方法 I never use the property, because I don't know what is their benefit. 我从不使用该属性,因为我不知道它们有什么好处。

here is example. 这是例子。 there is no need for properties. 不需要属性。

<?php 

    class Foo 
    {
         // don't have to use property   public $price;  public $qty;

        public function my_income($qty, $price)
        {

            return ($qty * $price);

        }

    }

    $foo = new Foo();

    echo $foo->my_income(20, 40);
     ?>

but I read there are something called 'getter setter' that is concern so much on properties like this 但我读到有一种叫做“ getter setter”的东西,在这样的属性上非常受关注

class Foo
{
    private $bar;

    public function getBar()
    {
        return $this->bar;
    }

    public function setBar($bar)
    {
        $this->bar = $bar;
    }
}

$foo = new Foo();
$foo->setBar(1);

I don't know why they worry about properties so much. 我不知道他们为什么这么担心房地产。 it's not make sense for me. 对我来说这没有意义。 if you want to use variable, you just assign variable outside of class or put value in function argument. 如果要使用变量,则只需在类外部分配变量或将值放入函数参数中即可。

There's no reason to use properties in that specific example, but object properties are hugely useful. 在该特定示例中没有理由使用属性,但是对象属性非常有用。

For instance, a Person class with someone's name and age: 例如,具有某人姓名和年龄的Person类:

class Person {

    private $name;
    private $age;

    public function __construct($name, $age) {
        $this->name = $name;
        $this->age = $age;
    }

    public function about() {
        return $this->name . " is " . $this->age . " years old";
    }
}

$joe = new Person("Joe", 42);
$alice = new Person("Alice", 27);
echo $joe->about(); // "Joe is 42 years old"
echo $alice->about(); // "Alice is 27 years old"

By grouping the data for the person together in one place (the object), we make it much easier to pass that data around. 通过将一个人的数据集中在一个位置(对象)中,我们可以更轻松地传递这些数据。

its all about encapsulating the data going around your script. 所有这些都与封装脚本中的数据有关。 This means giving it an 'owner'. 这意味着给它一个“所有者”。 This prevents data being accidentally overwritten or altered. 这样可以防止意外覆盖或更改数据。

A class encapsulates everything inside it. 一个类将所有内容封装在其中。 Everything gets a 'visibility', so private , protected , public . 一切都有“可见性”,因此是privateprotectedpublic

Consider this example: 考虑以下示例:

class Car {
        // no properties but some other methods you might want.
     public function someUtitity($x, $y) {
           return $x + $y;
     }
}



class CarBetter {
    private $colour;

    private $engineSize;

    public function __construct($colour, $engineSize) {
        $this->colour = $colour;
        $this->engineSize = $engineSize;
    }

    public function getColour() {
        return $this->colour;
    }

    public function getEngineSize() {
        return $this->engineSize;
    }

    public function setColour($colour) {
        $this->colour = $colour;
    }

    public function setEngineSize($es) {
        $this->engineSize = $es;
    }

}

Class Car is simply a utility class, it cannot actually represent an object of type Car because it can contain no data thats individual to that instance. Car类只是一个实用程序类,它实际上不能表示Car类型的对象,因为它不能包含该实例的单独数据。

Class CarBetter is a proper representation of the the Car model. CarBetter类是Car模型的正确表示。 It contains all the things you need to know about the car. 它包含您需要了解的所有有关汽车的知识。 They can only be changed by accessing the class directly, via its getters and setters. 只能通过通过其getter和setter直接访问该类来更改它们。

Now when I want to pass this instance of Car around my script its easy, and I can be sure that all its relevant data is encapsulated inside it. 现在,当我想在我的脚本中传递Car的这个实例时,它很容易,而且我可以确定所有相关数据都封装在其中。 (Think capsule with spacemen in it if it makes it easier). (如果这样更容易,请考虑在其中装有太空人的太空舱)。

Now, I can have multiple instances of an object CarBetter , each with its own specific data. 现在,我可以有一个对象CarBetter多个实例,每个实例都有其自己的特定数据。 None of that data can be changed unless its accessed directly through the class methods. 除非直接通过类方法访问,否则不能更改任何数据。

The problem: data structures and methods that can work on them. 问题是:可以使用的数据结构和方法。

In your case you have two values which appear to belong together, a quantity and a price. 在您的情况下,您有两个似乎属于在一起的值,即数量和价格。 You could always keep them around in two separate variables: 您可以始终将它们放在两个单独的变量中:

$qty = 20;
$price = 40;

And then all functions that use that data accept two arguments: 然后,所有使用该数据的函数都接受两个参数:

function income($qty, $price) { ... }

What about data structures that require a bit more than two fields, say a product description; 关于需要两个以上字段的数据结构呢? are you going to keep all those as individual variables? 您打算将所有这些保留为单独变量吗?

$product_name = 'Foo';
$product_price = 400;
$product_description = 'Lorem ipsum';
...

There are typically dozens of properties a product has; 产品通常具有数十种特性 will you keep an individual variable for each, and have each function accept dozens of arguments? 您会为每个变量保留一个变量,并让每个函数接受数十个参数吗?

function display_product($product_name, $product_price, $product_description, ...) { ... }

That's pretty impractical. 那是不切实际的。

So you define an actual data structure that you can carry around in a single variable: 因此,您定义了一个实际的数据结构,可以将其包含在一个变量中:

$product = [
    'name' => 'Foo',
    'price' => 400,
    ...
];

Now each function that works with product data only needs to accept a single argument which contains all the data: 现在,每个与产品数据一起使用的函数都只需接受一个包含所有数据的参数:

function tax(array $product) {
    return $product['price'] * 0.08;
}

But you still have disparate data definitions; 但是您仍然有不同的数据定义。 your $product array structure is defined over here, but there may be any number of functions defined over there, and if you change something about one or the other they might stop working together correctly. 您的$product数组结构是在此处定义的,但是那里可能定义了许多函数,如果您更改了一个或另一个函数,它们可能会停止正常工作。 So, let's bundle them up into one thing! 因此, 让我们将它们捆绑成一件事!

class Product {
    public $name;
    public $price;
    public $description;
    ...

    public function tax() {
        return $this->price * 0.08;
    }
}

The tax method works on specific product data, and it's bundled together with the actual data it's working on, so there's little chance of these two pieces diverging. tax方法适用于特定的产品数据,并且与正在处理的实际数据捆绑在一起,因此这两个部分几乎没有分歧的机会。

That's the basis of OOP and the use of properties. 是OOP和属性使用的基础 More interesting use cases can be build on that; 可以在此基础上构建更多有趣的用例。 eg polymorphism, which isn't really practical using only functions and variables. 例如多态,仅使用函数和变量实际上是不实际的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM