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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T01:12:28+00:00 2026-05-11T01:12:28+00:00

In PHP, if you define a class, and then instantiate an object of that

  • 0

In PHP, if you define a class, and then instantiate an object of that class, it’s possible to later arbitrarily add new members to that class. For example:

class foo {     public $bar = 5; }  $A = new foo; $A->temp = 10; 

However, I’d like the ability to make it impossible to add new members this way. Basically I want the class to ONLY have the members that are specified in its definition; if you try to set any other members, it fatally errors. The intent here is that I want to define a class as a very specific set of properties, and ensure that ONLY those properties exist in the class, so that the class contents are well-defined and cannot change later on (the values of each member can change, but not the members themselves).

I realize I can do this with the __set method, and simply have it fatal error if you try to set a member which doesn’t already exist, but that’s annoying to have to include in every class definition (although I could define each of my classes to extend a base class with that method, but that’s also annoying). E.g.:

class foo {     public $bar = 5;      private function __set($var, $val) {         trigger_error('Cannot dynamically add members to a class', E_USER_ERROR);     } } 

Is there any other (preferably more convenient) way to do this? Aside from modifying PHP itself to disallow this behavior?

  • 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. 2026-05-11T01:12:28+00:00Added an answer on May 11, 2026 at 1:12 am

    No. There’s no better way than __set in a base class — yet. This is a known problem and is planned to be addressed in the future:

    Introduce concept of “strict classes” that do not permit dynamic property creation

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

Sidebar

Related Questions

Is a global PHP CONSTANT available inside of a Class file? define('SITE_PATH', 'C:/webserver/htdocs/somefolder/'); Then
consider the following code scenario: <?php //widgetfactory.class.php // define a class class WidgetFactory {
My defs.php has a define that I need, and although I used require_once for
Let's say I release a code library as a standalone PHP class. Someone then
Basic PHP design questions: I want to define a couple of constants that are
I'm programming a website with PHP using Symfony2 . I defined a class MainContent
I am wondering if php methods are ever defined outside of the class body
it's all in index.php /* Define site root */ defined('DOCUMENT_ROOT') ? null : define('DOCUMENT_ROOT',realpath(dirname(__FILE__)));
C++ preprocessor #define is totally different. Is the PHP define() any different than just
Consider: <?php define('my_const', 100); echo <<<MYECHO <p>The value of my_const is {my_const}.</p> MYECHO; ?>

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.