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

  • SEARCH
  • Home
  • 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 1092463
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T23:41:10+00:00 2026-05-16T23:41:10+00:00

Which is the most semantically correct way to get/set variables when using OO PHP?

  • 0

Which is the most semantically “correct” way to get/set variables when using OO PHP?

From what I know, there are getters/setters, passing by reference and passing by value. Passing by value is slower than passing by reference, but passing by reference actually manipulates the memory of your variable. Assuming I would like to do this (or wouldn’t mind at least), which is more semantically correct/more efficient?

I’ve been using the getter/setter type when dealing with variables that are passed around the object. I find this to be semantically correct and easier to “read” (no long list function arguments). But I think it’s less efficient.

Consider this example (of course it’s contrived):

class bogus{
    var $member;

    __construct(){   
        $foo = "bar"
        $this->member = $foo;
        $this->byGetter();
        $this->byReference($foo);
        $this->byValue($foo);
    }

    function byGetter();{
        $baz =& $this->member; 
        //set the object property into a local scope variable for speed
        //do calculations with the value of $baz (which is the same as $member)
        return 1;
    }

    function byReference(&$baz){
        //$baz is already set as local.  
        //It would be the same as setting a property and then referencing it
        //do calculations with the value of $baz (same as $this->member)
        return 1;
    }

    function byValue($baz){
        //$baz is already set as local.  
        //It would be the same as setting a property and then assigning it
        //do calculations with the value of $baz 
        return 1;
    }
}
  • 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-16T23:41:11+00:00Added an answer on May 16, 2026 at 11:41 pm

    the most efficient way is if you do not use private/protected but public members, you accees those public members from outside like $instance->member

    also it’s deprecated to pass non-objects by reference so just don’t do it. also are all objects automatically passed by reference until you explcitly copy the memory ie. by using clone. just use a clean structure like this one and youÄll be fine 🙂

    class Example_SetterGetter
    {
        /**
         * @var stdClass
         */
        protected $_myObj;
    
        /**
         * A public constructor
         * 
         */
        public function __construct(stdClass $myObj = null)
        {
            if ($myObj !== null)
            {
                $this->setMyObj($myObj);
            }
        }
    
        /**
         * Setter for my object
         * @param stdClass $var
         * @return Example_SetterGetter
         */
        public function setMyObj(stdClass $var)
        {
            $this->_myObj = $var;
            return $this;
        }
    
        /**
         * Getter for my object
         * @return object
         */
        public function getMyObj()
        {
            return $this->_myObj;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i am using php/mysql. i have a product table . in which most product
I know how computer today stores negative integers, which most of the computers use
I am trying to figure out which is most appropriate. From the articles I
Which is the most effective way to sum data in the DataTable by a
using HTML5, would it be semantically correct to place an <article> element within a
I have a signup form of which most fields are pre-filled using user's Facebook
I'm wondering which is semantically and technically most optimal of my choices here. I've
I have a gridview that is loaded from a DataSet, in which most of
Anyone know of a way to get your hands on a specific version of
I am using the normal regular expression which most of us are using for

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.