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 1109331
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T02:13:01+00:00 2026-05-17T02:13:01+00:00

I have been coding in php and using codeigniter as well, never got the

  • 0

I have been coding in php and using codeigniter as well, never got the concept of getter and setter. What does it mean ?

  • 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-17T02:13:01+00:00Added an answer on May 17, 2026 at 2:13 am
    class Foo
    {
        protected $_bar;
        public function setBar($value) {
            $this->_bar = $value;
        }
        public function getBar()
        {
            return $this->_bar;
        }
    }
    

    The getter and setter here are the methods that allow access to the protected property $_bar. The idea is not to directly allow access to the property, but to control access to it through an API for your client code to consume. This way you can change the underlying state, while leaving the public facing methods as is. Thus, you are less likely to break a client if changes occur.

    Another reason to have them, is when you need to add logic before you get or set a property. For instance, a setter might validate and uppercase the value

    public function setBar($value)
    {
        if(!is_string($value)) {
            throw new InvalidArgumentException('Expected String');
        }
        $this->_bar = strtoupper($value);
    }
    

    or a getter may lazy instantiate some object

    public function getBar()
    {
        if($this->_bar === NULL) {
            $this->_bar = new Bar;
        }
        return $this->_bar;
    }
    

    Some people criticize getter and setters as boilerplate code, especially if they do not do anything than directly setting/getting the property they provide access to. That discussion is beyond scope though. Read more about it at

    • Getter/Setter aka Accessor/Mutator methods in Wikipedia
    • Is it really that wrong not using setters and getters? and
    • https://stackoverflow.com/search?q=getter+setter+php
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Ive never heard of this before, and I have been coding in PHP for
i mainly use PHP and I have been coding using it for a while.
I have been using php for a while but haven't taken the time to
Greetings, Well I am bewildered. I have been tasked with updating a PHP script
I have been coding my PHP applications in Notepad. I want to take it
I've been coding php for a while now and have a pretty firm grip
I've been coding a website for the past 6 months using HTML/CSS/PHP/MySQL/Javascript. I'm coding
In an php-based application I've been coding, I have the following small block of
I have been coding regurlarly in C++ in the past months. I am getting
I've been coding a very sizable project and I have run into trouble with

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.