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

The Archive Base Latest Questions

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

In my CakePHP 2 application I have such vendor. I need to create an

  • 0

In my CakePHP 2 application I have such vendor. I need to create an instance of this vendor class inside my controller class. So I will use that instance inside my controller’s different functions.

App::import('Vendor', 'fancyVendor', array('file' => 'fancyVendor.php'));

class MyController extends AppController {

    public $fancyVendor;

    function beforeFilter() {
       $fancyVendor = new fancyVendor();
       $fancyVendor->setValue("12");
    }

    function showMe() {
       echo $fancyVendor->getValue();
    }
}

Inside my showMe function, I can’t get the value that I set inside my beforeFilter function. Is there a proper way to instantiate it?

  • 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-16T02:56:40+00:00Added an answer on June 16, 2026 at 2:56 am

    You need to learn about scope. You have initialised a variable in the beforeFilter() scope and then trying to use it in the showMe scope. The two are completely different.

    You can make a variable that is scoped to the entire class, normally called a property…

    function beforeFilter() {
       $this->fancyVendor = new fancyVendor();
       $this->fancyVendor->setValue("12");
    }
    
    function showMe() {
       echo $this->fancyVendor->getValue();
    }
    

    Another point to note is that you can use the App::uses() method to load the class. According to your naming it would work. (class is lazy loaded this way)

    App::uses('fancyVendor', 'Vendor');
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In my CakePHP application, I have a model like this: class Duck extends AppModel
In my CakePHP application, I have a view with this markup: <div class=copy id=share-copy></div>
I'm building a CakePHP application and I have an API controller. This holds some
I have a CakePHP application that uses a number of vendor classes, including one
I have built an application in CakePHP that lists businesses. There are about 2000
I have developed a CakePHP application that I have now deployed to my production
I am following the CakePHP ACL tutorial http://book.cakephp.org/2.0/en/tutorials-and-examples/simple-acl-controlled-application/simple-acl-controlled-application.html I have a controller called ImagesController
Background I have a CakePHP application that lives in /m/ . I want to
In my CakePHP application I have an email form I have made myself that
In my CakePHP application layout I have a section for latest posts that needs

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.