Results 1 to 6 of 6

Thread: Interrupting Class Calls

  1. #1
    Join Date
    Sep 2009

    Interrupting Class Calls

    Hi there, i'm not sure 100% how to phrase the question and explain what i'm trying to do, so bare with me.

    Basically, i'd like to try and interupt a function call or class call and do some checks first. The best way I can think to do so is to call a function that does its checks and returns the function or class e.g:

    PHP Code:
    function getClass($className) {
    //do stuff
    return new $className;

    $class getClass("className"); 
    That works in its simplest format, but of course classes and functions have arguments sent to them and this poses a problem, as the possibilities for the different arguments are endless.

    I thought there'd be an easy way around this using func_get_args. However I don't know how to then send the arguments to the function or class other than in an array. So the only solution i've been able to come up with is to just accept a single argument as an array and then use the list function, e.g:

    PHP Code:
    class className {
        public function 
    __construct($args) {
    $this->option1$this->option2$this->option3) = $args;

    getClass() {
    $args func_get_args();

    //The class name should be the first paramater and we don't want to send it to the class.
    $className array_shift($args);

    //do stuff
    return new $className($args);

    $class getClass("className","someOption","anotherOption"); 

  2. #2
    Join Date
    Sep 2009
    Sorry I did the second snippet of code and totally forgot to finish what I was saying.

    The problem I have with this solution is that it makes things slightly messier as i'd have to check for required arguments rather than the function already expecting it.

    What i'm actually wanting to do is check for existances of an object first. For instance, I've found I may sometimes call the object for a product twice when I try to get all the cart items and all the products on a certain page.

    So in this instance, the function would check if it exists and if so i'd return the existing object. That way I don't do sql queries multiple times and if I modify the product the old data won't still be present in the other instance of the object.

  3. #3
    Join Date
    Aug 2009
    I am not sure, if this will solve you problem, but try making your class a "singleton":


    "the singleton pattern is a design pattern that is used to restrict instantiation of a class to one object."

    Support Chat software for your website

  4. #4
    Join Date
    Aug 2004
    Or you might want to create some sort of collection object (registry pattern?) to store each such object, and provide it with the necessary logic to control duplication. This way your more specific objects would not need to be singletons, with the testing and re-usability issues that entails. (Even if you decide to make the containing "collection" object a singleton, at least you are limiting that pattern to just that one class.)
    "Well done....Consciousness to sarcasm in five seconds!" ~ Terry Pratchett, Night Watch

    How to Ask Questions the Smart Way (not affiliated with this site, but well worth reading)

    My Blog
    cwrBlog: simple, no-database PHP blogging framework

  5. #5
    Join Date
    Sep 2009
    Woops I read your reply the other day, then started playing with it again and forgot to reply!

    I think what you've described is basically what i'm trying to do. I've got a "cache" class and the idea is to pass each class call through this function which checks to see if it's already stored in the cache class.

    What you described doesn't really solve my problem of passing the right arguments through to the class again if I do need to create it.

  6. #6
    Join Date
    Aug 2004
    This is the best I could think of, but I can't say I'm crazy about it. It requires that you use a recent enough version of PHP that you can instantiate a class via a variable for the class name. (IIRC, you can't do that before 5.1 or 5.2?)
    PHP Code:
    interface iParams
       public function 

    Foo implements iParams
       public function 
    __construct($var1=null$var2=null// all args optional
    $this->var1 $var1;
    $this->var2 $var2;
       public function 
    is_array($argArray) or count($argArray) != 2)
             throw new 
    Exception("Must be 2 args");
    $this->var1 $argArray[0];
    $this->var2 $argArray[1];

       public function 
    func_num_args() == 0)
             throw new 
    Exception("no class name");
    $args func_get_args();
    $className array_shift($args);
    $this->$className = new $className;
    is_a($this->$className'iParams') && !empty($args))
       private function 

    // TEST
    $cache = new MyCache();
    $test $cache->getClass('Foo''value 1''value 2');
    "<pre>";print_r($test);echo "</pre>";
    Exception $e)
    Last edited by NogDog; 01-15-2010 at 01:51 PM.
    "Well done....Consciousness to sarcasm in five seconds!" ~ Terry Pratchett, Night Watch

    How to Ask Questions the Smart Way (not affiliated with this site, but well worth reading)

    My Blog
    cwrBlog: simple, no-database PHP blogging framework

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

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



X vBulletin 4.2.2 Debug Information

  • Page Generation 0.10381 seconds
  • Memory Usage 2,925KB
  • Queries Executed 13 (?)
More Information
Template Usage (33):
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_global_above_footer
  • (1)ad_global_below_navbar
  • (1)ad_global_header1
  • (1)ad_global_header2
  • (1)ad_navbar_below
  • (1)ad_showthread_firstpost_sig
  • (1)ad_showthread_firstpost_start
  • (1)ad_thread_first_post_content
  • (1)ad_thread_last_post_content
  • (3)bbcode_php
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)headinclude_bottom
  • (6)memberaction_dropdown
  • (1)navbar
  • (4)navbar_link
  • (1)navbar_moderation
  • (1)navbar_noticebit
  • (1)navbar_tabs
  • (2)option
  • (6)postbit
  • (6)postbit_onlinestatus
  • (6)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available (6):
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files (26):
  • ./showthread.php
  • ./global.php
  • ./includes/class_bootstrap.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/functions_navigation.php
  • ./includes/class_friendly_url.php
  • ./includes/class_hook.php
  • ./includes/class_bootstrap_framework.php
  • ./vb/vb.php
  • ./vb/phrase.php
  • ./includes/functions_facebook.php
  • ./includes/functions_calendar.php
  • ./includes/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_notice.php
  • ./packages/vbattach/attach.php
  • ./vb/types.php
  • ./vb/cache.php
  • ./vb/cache/db.php
  • ./vb/cache/observer/db.php
  • ./vb/cache/observer.php 

Hooks Called (71):
  • init_startup
  • friendlyurl_resolve_class
  • init_startup_session_setup_start
  • database_pre_fetch_array
  • database_post_fetch_array
  • init_startup_session_setup_complete
  • global_bootstrap_init_start
  • global_bootstrap_init_complete
  • cache_permissions
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • load_show_variables
  • load_forum_show_variables
  • global_state_check
  • global_bootstrap_complete
  • global_start
  • style_fetch
  • global_setup_complete
  • showthread_start
  • showthread_getinfo
  • strip_bbcode
  • friendlyurl_clean_fragment
  • friendlyurl_geturl
  • forumjump
  • cache_templates
  • cache_templates_process
  • template_register_var
  • template_render_output
  • fetch_template_start
  • fetch_template_complete
  • parse_templates
  • fetch_musername
  • notices_check_start
  • notices_noticebit
  • process_templates_complete
  • friendlyurl_redirect_canonical
  • showthread_post_start
  • showthread_query_postids
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • memberaction_dropdown
  • tag_fetchbit
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • build_navigation_data
  • build_navigation_array
  • check_navigation_permission
  • process_navigation_links_start
  • process_navigation_links_complete
  • set_navigation_menu_element
  • build_navigation_menudata
  • build_navigation_listdata
  • build_navigation_list
  • set_navigation_tab_main
  • set_navigation_tab_fallback
  • navigation_tab_complete
  • fb_like_button
  • showthread_complete
  • page_templates