简体   繁体   English

PHP getter和setter

[英]PHP getter and setter

I have a class: 我有一节课:

class Test
{
    public $AppCount;
    public $Apps;

    // When $AppCount is accessed I want to return count( $this->Apps )
}

When I access property $AppCount , I want to return count( $this->Apps ) . 当我访问属性$AppCount ,我想返回count( $this->Apps )

Rather than having to declare an exposing function for this property and making it private, can I use a getter function like C# and Java have? 我不是必须为此属性声明一个公开函数并将其设为私有,而是可以使用像C#和Java这样的getter函数吗?

Obviously the __get is not what i want in this case as the property does already exist. 显然,在这种情况下, __get不是我想要的,因为该属性已经存在。

For the comments 对于评论

I have this and it does not run the function when i try and access the property: 我有这个,当我尝试访问该属性时它不运行该功能:

class ProjectSettingsViewModel
{
    public $ProjectAppCount = 0;
    public $ProjectApps = array();

    public function __get( $property )
    {
        switch( $property )
        {
            case "ProjectAppCount":
                return count( $this->ProjectApps );
                break;
        }
    }
}

If the code seems okay, it must be something else going wrong. 如果代码看起来没问题,那一定是其他问题。

Unfortunately, PHP does not have the getter and setter syntax you are referring to. 不幸的是,PHP没有你所指的getter和setter语法。 It has been proposed , but it didn't make it into PHP (yet). 它已被提出 ,但它还没有进入PHP(尚未)。

First of all, __get() is only executed if you are trying to access a property that can't be accessed (because it is protected or private , or because it does not exist). 首先,只有在尝试访问无法访问的属性时才会执行__get() (因为它是protectedprivate ,或者因为它不存在)。 If you are calling a property that is public , __get() will never be executed. 如果要调用public属性,则永远不会执行__get()

However, I would not suggest using PHP's magic getters and setters unless you really have no other choice. 但是,除非你真的别无选择,否则我不建议使用PHP的魔法吸气剂和设定器。 They are slower, tend to become a long if/elseif/else mess very quickly, and code completion (using a smart IDE) will not work. 它们速度较慢,很快就会成为一个很长的if / elseif / else混乱,并且代码完成(使用智能IDE)将无法正常工作。 Normal methods are a lot simpler and easier to understand. 普通方法更简单易懂。 You should also read this answer . 你也应该阅读这个答案

I have used the __get() and __set() methods for a while (instead of manually created getFoo() and setFoo() methods), because it seemed like a good idea, but I've changed my mind after a short time. 我已经使用了__get()__set()方法一段时间(而不是手动创建的getFoo()setFoo()方法),因为它似乎是一个好主意,但我在短时间内改变了主意。

So, in your case, I recommend writing normal methods: 因此,在您的情况下,我建议编写常规方法:

<?php

class Test
{
    /**
     * @var App[]
     */
    private $apps;

    /**
     * Returns an array containing all apps
     *
     * @return App[]
     */
    public function getApps()
    {
        return $this->apps;
    }

    /**
     * Returns the number of apps
     *
     * @return integer
     */
    public function getAppsCount() //or call it countApps()
    {
        return count($this->apps);
    }
}

Untested.. Should work though. 未经测试..应该可以工作。 Using more magic methodology. 使用更多魔术方法。

class ProjectSettingsViewModel
{
    protected $ProjectAppCount = 0;
    public $ProjectApps = array();

    public function __get( $property )
    {
        switch( $property )
        {
            case "ProjectAppCount":
                return count( $this->ProjectApps );
                break;
        }
    }
}

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

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