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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T07:29:22+00:00 2026-05-18T07:29:22+00:00

I’m adding a clearance section to a website and I’m running into an annoying

  • 0

I’m adding a clearance section to a website and I’m running into an annoying small issue with PDO. I’ve created a ClearanceItem class to hold information on an individual item. All of the properties of the item are protected and I’ve created accessors and mutators for them. I’ve created the class this way for two reason:

  1. I can verify the properties as they are set. This is important for me as the database is populated on a schedule from Excel files the client maintains. Using the mutators I can verify the information in the excel files is within certain ranges.
  2. As the site is in both English and French the accessors can format data (such as prices) correctly depending on the language for the view.

I want to be able to loop over a PDO resultset, getting a new instance of my ClearanceItem class automatically for each row. This is easily done by setting the fetch mode or using PDOStatement::fetchObject(). I’m having problems though with the fact that the properties are protected. It seems that when PDO creates a new instance of a class it uses something called Reflection Injection to set the properties. This means the protected properties are set directly by PDO, bypassing the mutators I’ve created. Because of this properties that should be numbers (and which are set as such by the mutators) are set as strings. Worst of all is that I’m using MySQL GROUP_CONCAT to return info on multiple stores and inventory as a single string, which the mutator should then split apart into an array. Because PDO directly sets this property it ends up as a string.

I can easily get over this problem by having a factory method to create a new instance of ClearanceItem from an array returned from a resultset row. This seems like an unnecessary extra step though. Does anyone know if it’s possible to have PDO return an instance of a class for each row in a recordset while having it respect property visibility, using the mutators to set the properties?

  • 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-18T07:29:22+00:00Added an answer on May 18, 2026 at 7:29 am

    I just verified, it WILL call the function __set() for each property set.

    class test {
      function __construct() {
        print "Constructor\n";
        var_dump(func_get_args());
      }
      function __get($x) {
        print "In get\n";
        var_dump($x);
      }
      function __set($x, $y) {
        print "In set\n";
        var_dump($x);
      }
    }
    
    $m = getMasterPDODB();
    $stm = $m->prepare("SELECT * FROM users");
    if( $stm && $stm->execute() ) {
      while( $cls = $stm->fetchObject("test") ) {
        var_dump($cls);
      }
    }
    

    gave me

    Constructor
    
    array
      empty
    
    In set
    
    string 'id' (length=2)
    
    In set
    
    string 'active' (length=6)
    
    In set
    
    string 'email' (length=5)
    
    In set
    
    string 'passphrase' (length=10)
    
    In set
    
    string 'details' (length=7)
    
    object(test)[5]
    
    Constructor
    
    array
      empty
    
    In set
    
    string 'id' (length=2)
    
    In set
    
    string 'active' (length=6)
    
    In set
    
    string 'email' (length=5)
    
    In set
    
    string 'passphrase' (length=10)
    
    In set
    
    string 'details' (length=7)
    
    object(test)[6]
    
    Constructor
    
    array
      empty
    
    In set
    
    string 'id' (length=2)
    
    In set
    
    string 'active' (length=6)
    
    In set
    
    string 'email' (length=5)
    
    In set
    
    string 'passphrase' (length=10)
    
    In set
    
    string 'details' (length=7)
    
    object(test)[5]
    
    Constructor
    
    array
      empty
    
    In set
    
    string 'id' (length=2)
    
    In set
    
    string 'active' (length=6)
    
    In set
    
    string 'email' (length=5)
    
    In set
    
    string 'passphrase' (length=10)
    
    In set
    
    string 'details' (length=7)
    
    object(test)[6]
    
    Constructor
    
    array
      empty
    
    In set
    
    string 'id' (length=2)
    
    In set
    
    string 'active' (length=6)
    
    In set
    
    string 'email' (length=5)
    
    In set
    
    string 'passphrase' (length=10)
    
    In set
    
    string 'details' (length=7)
    
    object(test)[5]
    

    And since my details is a json object, i could parse it from there into the fields needed. You can just have it call the mutators that you have created from there. This seems the best way without doing crazy ninja magic.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I used javascript for loading a picture on my website depending on which small
I have a French site that I want to parse, but am running into
I am currently running into a problem where an element is coming back from
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example

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.