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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 19, 20262026-06-19T00:15:23+00:00 2026-06-19T00:15:23+00:00

In a CakePHP 2.2 app, I’m using class constants in a Model for some

  • 0

In a CakePHP 2.2 app, I’m using class constants in a Model for some internal configuration. The following issue came up.

Short version:

Cake’s lazy class loading will not be triggered by a static call to the Model class.

If the first access to a Model in a Controller is

MyModel::SOME_CONST // fails

the class will be unknown. If any instance of the class is used before, it’s fine:

$this->MyModel->something();
MyModel::SOME_CONST // works

Not knowing about the details of the lazy loading implementation:

Question: Is this something that is impossible to fix? If so, why? How do I then best work around it in my App myself (wrap consts in a function)? Or is there a chance to improve the lazy loading so that it works with static access, too?

Long version with code:

In order to test the different cases, I made a small test App with 1 Model and 1 Controller:

Model/Post.php:

<?php
class Post extends AppModel {
    public $useTable = false; // Don't bother with a DB

    const FOO = "foo";

    public $bar = "bar";
}

Controller/PostsController.php:

<?php
class PostsController extends AppController {
    public function constant() {
        debug(Post::FOO);
    }

    public function variable() {
        debug($this->Post->bar);
    }

    public function variableFirst() {
        debug($this->Post->bar);

        debug(Post::FOO);
    }
}

Accessing the three controller actions through the browser, the different cases can now be tested.

1) accessing the Model constant (at /posts/constant):

Error: Class ‘AppModel’ not found

2) accessing the Model variable (at /posts/variable):

‘bar’

3) accessing the Model constant AFTER a variable (at /posts/variable):

‘bar’

‘foo’

  • 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-19T00:15:24+00:00Added an answer on June 19, 2026 at 12:15 am

    lazyloading works with normal class calls as well as static calls IF you correctly approach it.

    Correctly means, that you always have to App::uses() all used classes at the top of your file

    for AppModel in a model file:

    App::uses('AppModel', 'Model');
    
    class Post extends AppModel {}
    

    see the core files for details.

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

Sidebar

Related Questions

I have an issue with using the following code in my CakePHP app. In
I'm using Uploadify to handle uploads in my CakePHP app. Some uploads work fine
Please have a look at this validation array in my cakephp app for model
CakePHP APP folder contains following folders: - config - controllers - libs - locale
I'm using Vagrant + Chef Solo to set up a CakePHP app. I'd like
I am trying to test a plugin model in a CakePHP app. The model
In my cakephp app I have an Option model. In my option/index view I
For some reason I am unable to read any cookies from my CakePHP app
I've been developing a cakephp app (2.2) on my local machine using wamp. I'm
Ok, this is definitely weird. I have a cakephp app I'm using. I've removed

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.