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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T04:17:06+00:00 2026-05-11T04:17:06+00:00

I currently have my PHP class variables set up like this: class someThing {

  • 0

I currently have my PHP class variables set up like this:

class someThing {      private $cat;     private $dog;     private $mouse;     private $hamster;     private $zebra;     private $lion;      //getters, setters and other methods } 

But I’ve also seen people using a single array to store all the variables:

class someThing {      private $data = array();      //getters, setters and other methods } 

Which do you use, and why? What are the advantages and disadvantages of each?

  • 1 1 Answer
  • 4 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. 2026-05-11T04:17:06+00:00Added an answer on May 11, 2026 at 4:17 am

    Generally, the first is better for reasons other people have stated here already.

    However, if you need to store data on a class privately, but the footprint of data members is unknown, you’ll often see your 2nd example combined with __get() __set() hooks to hide that they’re being stored privately.

    class someThing {      private $data = array();      public function __get( $property )     {         if ( isset( $this->data[$property] ) )         {             return $this->data[$property];         }         return null;     }      public function __set( $property, $value )     {         $this->data[$property] = $value;     } } 

    Then, objects of this class can be used like an instance of stdClass, only none of the members you set are actually public

    $o = new someThing() $o->cow = 'moo'; $o->dog = 'woof'; // etc 

    This technique has its uses, but be aware that __get() and __set() are on the order of 10-12 times slower than setting public properties directly.

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

Sidebar

Related Questions

I currently have a error-system like this on my header.php: include('class.error.php'); Errsys::disable_default(); Errsys::enable_logging('errors.dat'); So,
I currently have a .htaccess file set up with this rule: RewriteRule ^([a-zA-Z0-9_-]+)$ user\.php?user=$1
I'm currently working on an OO PHP application. I have a class called validation
We currently have a working php mail script, this works fine and as we
We currently have an error catching script for all our PHP sites. This script
I currently have this piece of code: NSString *strURL = [NSString stringWithFormat:@http://www.sample.com /phpFile.php?firstname=%@,txtfirstName.text]; NSString
Im currently building a php framework... again. I have a class called config. its
I currently have a custom session handler class which simply builds on php's session
I have a class that sets various session variables. After I set the session
I currently have a PHP form that uses AJAX to connect to MySQL and

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.