1

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 ).

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?

Obviously the __get is not what i want in this case as the property does already exist.

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.

Jimmyt1988
  • 20,466
  • 41
  • 133
  • 233
  • You can use `__get` with properties that exist – John Conde May 16 '14 at 13:12
  • You *can* use the magic `__get` method for this, but please just create a `Test::getCount()` method. It's called make for a reason – PeeHaa May 16 '14 at 13:13
  • When i use __get, it does not seem to run when the property exists? I'll provide my code... – Jimmyt1988 May 16 '14 at 13:14
  • If you want the number of elements in an array, why do you have a property declared? Remove the property and use a magic method, or just use a `getCount()` method. –  May 16 '14 at 13:18
  • 3
    1) It works if you make it `private` / `protected`. 2) That property is useless if you calculate it any way 3) But again. Staph using magic in your code. – PeeHaa May 16 '14 at 13:18
  • That's the one, cheers. just had to make the property protected/private – Jimmyt1988 May 16 '14 at 13:19
  • 1
    I posted answer, but I second @PeeHaa .. Using magic in your code dangerous. – Zander Rootman May 16 '14 at 13:19
  • Thanks for your concern! The property has to be protected, otherwise the public one overrides it... if you edit your answer to the property being protected then i can accept it... but PeeHaa has the correct answer. I prefer using the getter and setter method, it has a clean markup. and my case is a good case for using it. – Jimmyt1988 May 16 '14 at 13:20
  • 1
    Yep.. Like I said. He is correct. Credits to @PeeHaa – Zander Rootman May 16 '14 at 13:24

2 Answers2

3

Unfortunately, PHP does not have the getter and setter syntax you are referring to. It has been proposed, but it didn't make it into PHP (yet).

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). If you are calling a property that is public, __get() will never be executed.

However, I would not suggest using PHP's magic getters and setters unless you really have no other choice. They are slower, tend to become a long if/elseif/else mess very quickly, and code completion (using a smart IDE) will not work. 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.

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);
    }
}
Community
  • 1
  • 1
Nic Wortel
  • 11,155
  • 6
  • 60
  • 79
0

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;
        }
    }
}
Jimmyt1988
  • 20,466
  • 41
  • 133
  • 233
Zander Rootman
  • 2,178
  • 2
  • 17
  • 29