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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T02:07:56+00:00 2026-06-06T02:07:56+00:00

This is something that’s been bugging me with PHP for a while now and

  • 0

This is something that’s been bugging me with PHP for a while now and I still can’t figure out how I would do it. Basically at the moment for my SQL class I’m dumping the results in an array and converting them to an object to give something like this:

$first_post = $Post->find(1);

(I’ve also been trying to get the syntax to look like: $Post::find(1); but that’s a different and purely aesthetic issue altogether)

and then you can use first_post like so:

$first_post->title;

This is done by just converting the contents of $post->find(1) to an object like so:

$first_post = (object) array("title" => "blah");

and I know that instantiates a new STDClass but I can’t figure out how to bind methods to that new instance of STDClass. It’s mainly so I can do stuff like:

$posts = $Post->all();
$last_post = $posts->last();
$specific  = $posts->find("name" => "hello");

Any ideas how I would get PHP to do something like this?

  • 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-06T02:07:57+00:00Added an answer on June 6, 2026 at 2:07 am

    (I’ve also been trying to get the syntax to look like: $Post::find(1); but that’s a different and purely aesthetic issue altogether)

    When using the scope resolution operator you are accessing static methods and/or constants. These are methods that work on the class rather than an instance of the class. For example:

    // the find method retuns a new Post instance and 
    // does not try to access $this
    $post = Post::find(1);  
    

    As for creating an object for the post from an array, it’s probably better to either create your own Post class and populate it accordingly or use one of the database functions that return the table data as an object rather than array such as mysqli_result::fetch_object or mysql_fetch_object.

    If creating your own Post class, you can store the database information in an array and use the __call(), __get() and __get() Magic Methods to access that data. For example:

    class Post() {
        protected $data;
        
        public function __construct($data) {
            $this->data = $data;
        }
        
        // access data with a getFoobar() style call
        public function __call($name, $args) {
            if (substr($name, 0, 3) == 'get') {
                $key = strtolower(substr($name, 3));
                if (array_key_exists($key, $this->data)) {
                    return $this->data[$key];
                }
            }
            
            $class = get_class($this);
            throw new Exception("Call to undefined method $class::$name");
        }
        
        // access data like a class property
        public function __get($key) {
            if (array_key_exists($key, $this->data)) {
                return $this->data[$key];
            }
            
            throw new Exception("Attempt to access non-existant property $class::$name");
        }
        
        // set the data like a class property
        public function __set($name, $value) {
            $this->data[$name] = $value;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is something that's been bugging me for many years: why most online services
This is something that's bugged me for awhile now: def test (*args, **kwargs): print
This is something that's I've wanted to know recently, mostly out of curiousity. I'm
This is something that I solved using reflection, but would like to see how
So this is something that I have not been able to find any documentation
Is this something that can be used for highly secure information or should it
Can someone explain why everything in WPF is blurry? Is this something that can
Is this something that can be done more efficiently than with a vertex array?
This is something that I don't see much discussed. I'm developing a software that
this is something that i don't want to program, but i was looking for

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.