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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T12:37:08+00:00 2026-06-17T12:37:08+00:00

I used cakePHP which has a nice feature where the model (if present is

  • 0

I used cakePHP which has a nice feature where the model (if present is already created as a property within your controllers, so effectively I would have access to a property named $this->model_name within my controller without having the create an instance of the model object.

From what I understand all properties must be defined in a class to be able to use it, so is there another way for me to be able to complete the above?

  // Sample code:
  <?php
  class controller {
        public function create_model($model_name) {
              // Assuming that I have spl_autoload enabled to achieve the below:
              $this->$$model_name = new model_name();      
        }
  }
  • 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-17T12:37:09+00:00Added an answer on June 17, 2026 at 12:37 pm

    You can do things like this with magic methods (check out _set() and _get() )

    Here is some sample code:

    class Controller
    {
        protected $models;
    
        public function __get($key)
        {
            return $this->models[$key];
        }
    
        public function __set($key, $value)
        {
            $this->models[$key] = $value;
        }
    }
    

    You can implement your own functionality in __set() and __get(). You can set data with $this->my_model = $something;.

    Here is something that’s more tailored to your specific example:

    public function __get($key) // you will only need __get() now
        {
    
            if (array_key_exists($key, $this->models) && $this->models[$key] instanceof $key) { 
                return $this->models[$key];
            } else {
                $this->models[$key] = new $key;
                return $this->models[$key];
            }
    
        }
    

    So now, $this->my_model with try to instantiate my_model if it doesn’t exist, and return the current object if it exists. Maybe not the best solution, but added it here so you can understand how it works.

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

Sidebar

Related Questions

I am editing a website which someone has made in CakePHP, but I don't
I want to truncate my Database Table, with CakePHP Model, I have used $this->Model->deleteAll
I have added a new column to my table Attributes which already has (id
I have used cakePHP 1.2 and know how to use behaviors in it. using
My Question: What are some good examples of design patterns used in CakePHP? Why
I've followed the tutorial here: http://book.cakephp.org/view/1286/Sending-a-basic-message , which I have successfully used before with
I have seen in cakephp that foreach loop is used like this foreach($tags as
I'm writing an application in CakePHP that, for now, is to be used to
This used to work: $this->Model->find('all', array( 'fields' => 'Model.field/100 as name') but I now
I am a newbie to drupal, and have used cakephp and codeigniter frameworks earlier.

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.