+ Reply to Thread
Results 1 to 3 of 3

Thread: Scope and controller question

  1. #1
    Howler9443 is offline Member
    Join Date
    Oct 2007
    Posts
    30

    Default Scope and controller question

    Hello everyone,

    I'm still pretty new to ZF and I apologize if this is obvious or has been asked before. I've looked for an answer to this and I seem to be missing something here.

    I have a simple indexController.php file and in the indexAction, I wanted to call a non-action method so that I could organize my code a little better.

    basically I have something like...


    Code:
      public function indexAction()
        {
           someNonActionMethod();
        }

    However, when I make that call, I get an error "call to undefined function..."

    However, when I do this....


    Code:
      public function indexAction()
        {
           $this->someNonActionMethod();
        }
    It works. I know this is a scope issue, could someone explain why this is the case or should I be able to just call the method without the $this-> ??

    Thanks in advance

  2. #2
    lion21 is offline Junior Member
    Join Date
    Apr 2009
    Posts
    13

    Default

    I wd suggest you to learn object oriented programming first and then go for zf . Back to ur question , in a class you have public,private,protected ,static type of functions only and $this refers to the object created by ur class hence u need to use it.


    Quote Originally Posted by Howler9443 View Post
    Hello everyone,

    I'm still pretty new to ZF and I apologize if this is obvious or has been asked before. I've looked for an answer to this and I seem to be missing something here.

    I have a simple indexController.php file and in the indexAction, I wanted to call a non-action method so that I could organize my code a little better.

    basically I have something like...


    Code:
      public function indexAction()
        {
           someNonActionMethod();
        }

    However, when I make that call, I get an error "call to undefined function..."

    However, when I do this....


    Code:
      public function indexAction()
        {
           $this->someNonActionMethod();
        }
    It works. I know this is a scope issue, could someone explain why this is the case or should I be able to just call the method without the $this-> ??

    Thanks in advance

  3. #3
    Howler9443 is offline Member
    Join Date
    Oct 2007
    Posts
    30

    Default

    Hi Lions,

    Thanks for the reply.

    Actually I am pretty well versed in OO coding as I have done a good bit of coding in Java. Its my lack of PHP OO nuances that I am lacking.

    In Java a private method can be called either with the "this" keyword or without. Private class variables however must be called with "this" if you want to access them. Using "this" also removes any vagueness and ensures you are accessing the correct variables especially if you are shadowing variables, which really isn't that great of an idea to begin with. I was expecting that the "this" keyword would be optional in PHP as it is in Java and made a bad assumption and it didn't dawn on me that $this->somePrivateMethod() is required and not just optional.

    Thanks again

+ Reply to Thread

Similar Threads

  1. Question about controller names
    By sasquatch in forum Model-View-Controller (MVC)
    Replies: 4
    Last Post: 11-15-2009, 08:27 PM
  2. Basic Controller Architecture Question
    By joshuamc in forum Concepts, Ideas, Planning
    Replies: 1
    Last Post: 05-20-2009, 09:07 PM
  3. A question about Controller and View Connection!
    By youqing in forum General Q&A on Zend Framework
    Replies: 2
    Last Post: 08-25-2008, 04:19 PM
  4. Replies: 0
    Last Post: 01-18-2008, 09:55 AM
  5. Controller question
    By gikjl in forum General Q&A on Zend Framework
    Replies: 4
    Last Post: 11-18-2007, 11:52 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts