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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T13:35:08+00:00 2026-05-23T13:35:08+00:00

So I ran into a problem while building a class in which I was

  • 0

So I ran into a problem while building a class in which I was unable to set the property of the class directly, and instead had to set it during construction. Here is an example of what I was trying to do.

class foo
{
    private $con = Db::init();

    public function __construct()
    {
    }

    //continue class..
}

As you can see, I am just assigning a simple singleton PDO class to the property. This does not work, and I am forced to do the following.

class foo
{
    private $con;

    public function __construct()
    {
        $this->con = Db::init();
    }

    //continue class..
}

The first approach does not report any errors either. It just fails to continue execution. Any thoughts?

edit

The lack of errors may also be a Zen Cart thing.

  • 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-23T13:35:08+00:00Added an answer on May 23, 2026 at 1:35 pm

    What is happening here is the class is a structure, and the structure is compiled before your PHP Fiel is executed, as in the compile time PHP Does not instantiate any dynamic data, you cannot use dynamic data.

    For example:

    $function = 'hello';
    function $function(){}
    

    Within the compile time the variable ‘$function’ does not exists so it cannot be read, within your class PHP Has provided a function called __construct which is fired within run-time, meaning that the rest of the system’s dynamic data is available.

    class foo
    {
        protected $bar;
    
        public function __construct()
        {
            $this->bar = Db::Init();
        }
    }
    

    So the process is:

    • Compile Time
      • Class foo
      • variable bar
      • function __construct
    • Run Time
      • new foo found
      • internally create object
      • execute foo::__construct()
      • return foo

    That’s a simplified version of the process, there are some several ways to set objects to a class, you can do the regular approach as shown above

    you can inject by doing:

    public function __construct(Bar $bar)
    {
        $this->bar = $bar;
    }
    

    you can create a base class and extend:

    class DatabaseAccess
    {
        protected $db;
    
        public function __construct()
        {
            $this->db = Db::Init();
        }
    }
    

    and then do:

    class User extends DatabaseAccess
    {
        public function getUser($id)
        {
             $this->db->fetchRow('users',$id);
        }
    }
    

    }

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

Sidebar

Related Questions

While building my application with relational tables I ran into the following problem :
I've ran into a problem while trying to test following IRepository based on NHibernate:
A problem I ran into a while back I never found a good solution
I've ran into a really weird problem while working on a large project. I
I ran into this problem while developing in Objective-C for iOS, but this should
I ran into a problem while trying to parse an XML string returned from
So I ran into a problem today while working on my Android program. I
I ran into the problem that my primary key sequence is not in sync
I recently ran into a problem that I thought boost::lambda or boost::phoenix could help
I recently ran into a problem with a COM object that was using 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.