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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T09:58:29+00:00 2026-06-14T09:58:29+00:00

Here is my php class: class Category { private $cat_id; private $cat_name; private $cat_is_main;

  • 0

Here is my php class:

class Category {
    private $cat_id;
    private $cat_name;
    private $cat_is_main;
    private $cat_parent;

    function __get($key) {
        switch ($key) {
            case 'cat_id':
                return $this->cat_id;
            case 'cat_name':
                return $this->cat_name;
            case 'cat_is_main':
                return $this->cat_is_main;
            case 'cat_parent':
                return $this->cat_parent;
        }
    }

    function __set($key, $value) {
        switch ($key) {
            case 'cat_id':
                $this->cat_id = (int) $value;
                break;
            case 'cat_name':
                $this->cat_name = (string) $value;
                break;
            case 'cat_is_main':
                $this->cat_is_main = (bool) $value;
                break;
            case 'cat_parent':
                $this->cat_parent = (int) $value;
                break;
        }
    }
}
$conn = new mysqli($server, $username, $password, $dbname);
if ($result = $conn->query('SELECT cat_id, cat_name FROM categories WHERE cat_id = 1;')) {
    var_dump($result->fetch_object('Category'));
}

And I got:

object(Category)#5 (4) {
    ["cat_id":"Category":private]=> string(1) "1"
    ["cat_name":"Category":private]=> string(9) "test data"
    ["cat_is_main":"Category":private]=> string(1) "1"
    ["cat_parent":"Category":private]=> string(1) "0"
}

What I’m expecting is something like:

object(Category)#1 (4) {
    ["cat_id":"Category":private]=> int(1)
    ["cat_name":"Category":private]=> string(9) "test data"
    ["cat_is_main":"Category":private]=> bool(true)
    ["cat_parent":"Category":private]=> int(0)
}

It seem the mysqli_fetch_object() does not use my __set() method when create new object. It just some how set value for my private properties directly.
Is this normal in PHP? Is there anything else I can do to get what I want?
Thanks!

  • 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-14T09:58:30+00:00Added an answer on June 14, 2026 at 9:58 am

    When mysqli_fetch_object() creates an object, it basically creates a stdClass first with all of the attributes set and then switches the object’s class to the one you supplied and then runs the constructor. As you noticed, the __set() method doesn’t get called, but that is normal and just how the function works.

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

Sidebar

Related Questions

Here is the class: functions.php class buildPage { public function Set($var,$val){ $this->set->$var = $val;
Here's my Zend code: <?php require_once ('Zend\Form.php'); class Sergio_Form_registrationform extends Zend_Form { public function
Here's the code: <?php class Order extends Zend_Db_Table_Abstract { protected $_name = 'orders'; protected
Here's the code: <?php class Order extends Zend_Db_Table_Abstract { protected $_name = 'orders'; protected
Here is the content of file User.php <?php class My_Controller_Plugin_User extends Zend_Controller_Plugin_Abstract { public
I am trying to extend a PHP class without rewriting the whole thing. Here
PHP/MySQLisolating database access in class - how to handle multiple row Selects Here’s a
Here's what I'd like: Here's my code: <?php require_once ('Zend\Form.php'); class Sergio_Form_registrationform extends Zend_Form
I'm learning by reading this tutorial: Link Here's the code: <?php require_once 'Zend/Loader.php'; class
Iam using a php class to add event to google calender.But the class return

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.