php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #53229 Change of getters and setters
Submitted: 2010-11-02 20:44 UTC Modified: 2015-03-20 15:01 UTC
From: sjors dot vanleeuwen at itsxtreme dot com Assigned:
Status: Duplicate Package: Class/Object related
PHP Version: Irrelevant OS:
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If this is not your bug, you can add a comment by following this link.
If this is your bug, but you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: sjors dot vanleeuwen at itsxtreme dot com
New email:
PHP Version: OS:

 

 [2010-11-02 20:44 UTC] sjors dot vanleeuwen at itsxtreme dot com
Description:
------------
It would be very usefull to have getters in a way like c#.

Example:

Class Test {
  private $_a;
  private $_b;

  public function get a() {
    return $this->_a;
  }
}

this way the private $_b isn't available to the outside world which is the case with out of the box __get.

It also (in my opinion) gives your code a much cleaner look and you can do special things if needed in your functions too.

in the __get function you'll have to make if statements or other checks to get the same result.

You can also just use:

Example:

Class Test {
  private $_a;
  private $_b;

  public function a() {
    return $this->_a;
  }
}

but then you would have to use:
$test->a() instead of $test->a which gives a wrong assumption that you are calling a function which you are not, you are calling a property of your object.


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2010-11-04 17:03 UTC] uramihsayibok at gmail dot com
This is the umpteenth request for this feature. Look at the PHP RFC Wiki 
(http://wiki.php.net/rfc), specifically "Property get/set syntax" 
(http://wiki.php.net/rfc/propertygetsetsyntax).

Meanwhile, a protip for you:
1. Use a public $a and public $b.
2. Unset the variables in your constructor.
3. Implement __get and __set however you see fit. Example: use protected _getA() 
and _setA() methods.
 [2010-11-04 19:07 UTC] sjors dot vanleeuwen at itsxtreme dot com
Sorry for the 1000000th post of the request and ofcourse thanks for the tips, though not everyone of them applies to the cause.

Be hoping to see it soon and i might look into creating a patch for it in the meanwhile. Is there any way i can help developing it for the community?

Kind regards
 [2010-11-05 04:37 UTC] aharvey@php.net
-Status: Open +Status: Bogus
 [2010-11-05 04:37 UTC] aharvey@php.net
Have a look at the RFC, and if you come up with a patch, feel free to send it to the Internals mailing list for discussion.

Closing, since this is better dealt with via the RFC/mailing list process than in the bug tracker.
 [2015-03-20 15:01 UTC] levim@php.net
-Status: Not a bug +Status: Duplicate
 [2015-03-20 15:01 UTC] levim@php.net
This is a duplicate of bug #69262
 [2015-03-20 15:02 UTC] levim@php.net
Shoot, wrong window. This is a duplicate of bug #49526.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Mar 29 15:01:28 2024 UTC