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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T11:25:32+00:00 2026-06-12T11:25:32+00:00

class A { $props = array(‘prop1’, ‘prop2’, ‘prop3’); } How to convert above defined

  • 0
class A {
    $props = array('prop1', 'prop2', 'prop3');
}

How to convert above defined array into class properties ? The final result would be..

class A {
    $props = array('prop1', 'prop2', 'prop3');
    public $prop1;
    public $prop2;
    public $prop3;
}

I have tried this so far :

public function convert(){
        foreach ($this->props as $prop) {
            $this->prop;
        }
    }

Looks bit ugly as I am new to php

  • 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-06-12T11:25:33+00:00Added an answer on June 12, 2026 at 11:25 am

    You may use php magic methods __get and __set like this (study when and how are they invoked before implementing):

    class A {
        protected $props = array('prop1', 'prop2', 'prop3');
    
        // Although I'd rather use something like this:
        protected GetProps()
        {
            return array('prop1', 'prop2', 'prop3');
        }
        // So you could make class B, which would return array('prop4') + parent::GetProps()
    
        // Array containing actual values
        protected $_values = array();
    
        public function __get($key)
        {
            if( !in_array( $key, GetProps()){
                throw new Exception("Unknown property: $key");
            }
    
            if( isset( $this->_values[$key])){
                return $this->_values[$key];
            }
    
            return null;
        }
    
        public function __set( $key, $val)
        {
            if( !in_array( $key, GetProps()){
                throw new Exception("Unknown property: $key");
            }
            $this->_values[$key] = $val;
        }
    }
    

    And you would use it as normal properties:

    $instance = new A();
    $a->prop1 = 'one';
    $tmp = $a->undef; // will throw an exception
    

    It would be also nice if you would implement:

    • public function __isset($key){}
    • public function __unset($key){}

    so you’ll have consistent and complete class.

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

Sidebar

Related Questions

Say hypothetically I have a class... class Main { $prop1 = 2; $prop2 =
As this does not seam to work? class Site { private $PROPERTIES = array(
what would be the difference between this class Class1 { public string prop1 {
class A attr_accessor :dab .... end Now I have an array of instances of
I have a class with a property that is an array: class NewObject {
i just made a person class and defined 2 overloaded constructors as u see
If I have a class such as below (only with many more properties), is
Given two classes: class A { private $prop1; } class B extends A {
I want to loop over an array continuously on click. Extra props if you
I have a class that uses magic methods to store properties. Here is a

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.