Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6114737
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T15:00:36+00:00 2026-05-23T15:00:36+00:00

What strategies are available to creating accessors and mutators for a PHP class’ private

  • 0

What strategies are available to creating accessors and mutators for a PHP class’ private members? Is this suggestion good: http://cormacscode.wordpress.com/2009/01/22/read-only-object-variables-in-php-using-magic-methods/

<?php
class classWithReadOnlyVar
{
    private $readOnlyVar;

    public function __get($varName)
    {
        return $this->$varName;
    }

    public function __set($varName,$varValue)
    {
    }
}

What if some members needed a private, public, or protected property method?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T15:00:37+00:00Added an answer on May 23, 2026 at 3:00 pm

    First of all, __get, __set, etc. are defined public and you cannot have them otherwise. Magic methods should be used wisely as it takes about three times as long to make a call to a magic method than simply calling a class method.

    class A {
       public function __get($name) { ... }
    
       public function __getValue() { ... }     // <== is faster
    
    }
    

    Usually (normally, preferably), you will have your class members private or protected (never public) and have accessors and mutators to encapsulate them. Those accessors and mutator may be of any visibility, depending on what the user can do with the members. You may also have immutable classes by declaring only accessors for your members, which are initialized in the constructor only.

    So, your sample class should read

    class classWithReadOnlyVar {
       private $readOnlyVar;
    
       public function getReadonlyVar() {
         return $this->readOnlyVar;
       }
    
    }
    

    and should not use the magic methods.

    There may be many reasons to avoid using magic methods at all :

    1. they break code completion
    2. they are slower at run-time
    3. they make refactoring and maintenance a bit (lot) harder/complicated
    4. you can’t have a protected magic method
    5. etc.

    Class members

    Their visibility should be private or protected, it all depends if you want them accessible by inheritence. They should never be public as this breaks the OO paradigm.

    Example of a protected member:

    class A {
        protected $_name = 'A';
    
        public function getName() { return $this->_name; }
    }
    
    class B {
        protected $_name = 'B';   // getName() will not return 'B'
    }
    

    (without $_name being protected, this would not be possible, and no need to redefine the accessor)

    Accessors

    They should be protected or public. Having a private accessor makes no sense; a class should access it’s member directly. If the member needs processing, the class will know regardless when to call the accessor or not.

    Mutators

    They should be protected or public. As for the accessors, it makes no sense to have a private mutator… unless a very rare case when processing needs to be done internally. If a class member has no accessor, it should not have a mutator. It also makes no sense to have a mean to set a value without being able to get the value back somehow.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to know which php architecture strategies developers use in complex php applications
Are there other strategies available for auto-incrementing a primary key? So instead of the
What good practices and strategies are there for running regression tests in embedded environments
What are some effective strategies for preventing the use of my proprietary images? I'm
What are some strategies that people have had success with for maintaining a change
Do you have any strategies for retrofitting unit tests onto a code base that
I'm wondering what strategies people use for reduced sign on with legacy applications and
What are some strategies to achieve code separation in AJAX applications? I am building
Does anyone have any strategies/tips/traps for moving to Team System? Should it be done
I am curious what strategies folks have found for unit testing a data access

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.