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

The Archive Base Latest Questions

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

Why does this return an array instead of an object and how can I

  • 0

Why does this return an array instead of an object and how can I return an object?

Class MyClass {
    private $filename = '...';
    private $_ini_array;

    public function __construct() {
        // Get the config file data
        $ini_array = parse_ini_file( $filename, true );
        $this->_ini_array = $ini_array;
    }

    public function __get( $param ) {
        return $this->_ini_array[ $param ];
    }
}

called by…

$c = new MyClass();
var_dump( $c->db_pgsql );

returns…

array(6) {
  ["data"]=>
  string(4) "test"
  ...

and casting by…

return (object) $this->_ini_array;

returns…

object(stdClass)#2 (6) {
  ["data"]=>
  string(4) "test"
  ...

while I wish to return…

object(MyClass)#2 (6) {
  ["data"]=>
  string(4) "test"
  ...

Many Thanks!

Update. Solved.

I ended up writing the following class that pretty much accomplishes my goals. Please comment if you see any bad habits, sloppy code, etc.

class Config {
    private $config_filename = '../include/config.ini';


    public function __construct( array $array=null ){
        if ( $array ) {
            foreach ( $array as $key => $val ) {
                $this->$key = $val;
            }
        } else {
            $ini_array = parse_ini_file( $this->config_filename, true );

            foreach( $ini_array as $key => $val ){
                $this->$key = new self( $val );
            }
        }
    }


    public function __get( $param ) {
        return $this->$param;
    }
}

Which, with my particular test config file, produces an object that looks like…

VarDump: object(Config)#1 (3) {
    ["config_filename:private"]=>
    string(21) "../include/config.ini"
    ["heading1"]=>
    object(Config)#2 (3) {
        ["config_filename:private"]=>
        string(21) "../include/config.ini"
        ["str1"]=>
        string(4) "test"
        ["str2"]=>
        string(5) "test2"
    }
    ["heading2"]=>
    object(Config)#3 (2) {
        ["config_filename:private"]=>
        string(21) "../include/config.ini"
        ["str1"]=>
        string(9) "testagain"
    }
}

I would have preferred not to have recursively duplicated the [“config_filename:private”] property like it did. But I couldn’t conceive a way around it. So if you know of a workaround then I’d appreciate a comment.

Thanks for all the help to get me in the right direction.

  • 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-26T23:41:12+00:00Added an answer on May 26, 2026 at 11:41 pm

    Why does this return an array instead of an object[…]

    The only thing I see setting $this->_ini_array is the return value of parse_ini_file, which returns an array (an array of arrays).

    how can I return an object?

    You’ll need to iterate over the pertinent array and populate an object manually.

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

Sidebar

Related Questions

Why does this code always return 0? var possibleMatches = new Array(); $.getJSON('getInformation.php', function(data)
CGFloat* colors = CGColorGetComponents(hsbaColor.CGColor); Does this return a float, or an array of floats?
can someone please help me. why does this return an error: Dim stuff As
Why does this javascript return 108 instead of 2008? it gets the day and
Does this pattern: setTimeout(function(){ // do stuff }, 0); Actually return control to the
You can pass a boolean to json_decode to return an array instead of an
Why does this script return a pair of nulls? I'm using SQL Server 2008,
This is probably a quicky. Why does this code not return anything? import java.util.Scanner;
Simple question - what does this actually do? var oq = (ObjectQuery<TEntity>)L2EQuery; return ExecuteFirstorDefault<TEntity>(oq,
What does the following expression return in Java? Math.max(Float.POSITIVE_INFINITY, Double.POSITIVE_INFINITY); I saw this question

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.