Advertisement:

Author Topic: Using hooks within class objects  (Read 1095 times)

FredrickReuben

  • Newbie
  • *
  • Posts: 24
Using hooks within class objects
« on: June 04, 2016, 03:41:51 pm »
Hello good people, I am creating a plugin for my site, I would like to use hooks within class construct object. But I dont Know how to make it happen. here is a code am working with, your help will be appreciated. Thank you
Code: [Select]
<?php

/**
 * The file that defines the core plugin class
 *
 * A class definition that includes attributes and functions used across both the
 * public-facing side of the site and the admin area.
 *
 * @link       http://pixadrop.com
 * @since      1.0.0
 *
 * @package    easy_avatar
 * @subpackage easy_avatar/includes
 */

/**
 * The core plugin class. 
 *
 * This is used to define internationalization, admin-specific hooks, and
 * public-facing site hooks.
 *
 * Also maintains the unique identifier of this plugin as well as the current
 * version of the plugin.
 *
 * @since      1.0.0
 * @package    easy_avatar
 * @subpackage easy_avatar/includes
 * @author     Your Name <email@example.com>
 */
class easy_avatar_delete_user {

/**
 * The instance that's responsible for creating an instance of self
 * the plugin.
 *
 * @since    1.0.0
 * @access   protected
 * @var       $instance    store all instance.
 */
private static $instance;

/**
 * Define the core functionality of the plugin.
 *
 * Set the plugin name and the plugin version that can be used throughout the plugin.
 * Load the dependencies, define the locale, and set the hooks for the admin area and
 * the public-facing side of the site.
 *
 * @since    1.0.0
 */
public function __construct() {
        
osc_add_hook('delete_user', array($this'esa_delete_dir' ));
        
osc_add_hook('delete_user', array($this'esa_drop_row'));

}

    
/**
     * Singleton constructor.
     * @return an theme_images object.
     */
    
public static function newInstance() {
            if(!
self::$instance instanceof self) {
                    
self::$instance = new self;
            }
            return 
self::$instance;
    }

/**
 * Delete user avatart directory when use is removed
 * of the plugin.
 *
 * @since    1.0.0
 * @access   private
 */
     
public function esa_delete_dir($id){

     }

     
/**
 * Drop user table row when user is removed
 * of the plugin.
 *
 * @since    1.0.0
 * @access   private
 */
     
public function esa_drop_row($id){

     }

}

teseo

  • Hero Member
  • *****
  • Posts: 6169
Re: Using hooks within class objects
« Reply #1 on: June 04, 2016, 05:31:30 pm »
Hi,

Take a look at the docs:

https://doc.osclass.org/Hooks

Quote
osc_add_hook('hook_name', 'function_name');

Maybe this thread could give you some orientation as well:

http://forums.osclass.org/plugins-20/call-a-function-after-ad-is-posted

Regards

FredrickReuben

  • Newbie
  • *
  • Posts: 24
Re: Using hooks within class objects
« Reply #2 on: June 05, 2016, 12:09:58 am »
Thank you so much Teseo for the reply, I read the two links you recommended, I know how hooks work, but I don't know how to implement them within a class object, I tried 
Code: [Select]
osc_add_hook('hook_name', 'function_name'); but it does not work within class object, do you have any idea on how this could work.

teseo

  • Hero Member
  • *****
  • Posts: 6169
Re: Using hooks within class objects
« Reply #3 on: June 05, 2016, 08:44:27 am »
Hm... Interesting, indeed you cannot use:

Code: [Select]
osc_add_hook('hook_name', '$this->function_name');
Furthermore, I doubt that a __construct method is the right place to invoke a hook... ???

So I guess you need to do it indirectly, not inside the class but in index.php:

Code: [Select]
<?php
function esa_delete_user($id) {
    
easy_avatar_delete_user::newInstance()->esa_delete_dir($id);
    
easy_avatar_delete_user::newInstance()->esa_drop_row($id);
}

osc_add_hook('delete_user''esa_delete_user');
?>

Regards

« Last Edit: June 05, 2016, 10:42:46 am by teseo »

FredrickReuben

  • Newbie
  • *
  • Posts: 24
Re: Using hooks within class objects
« Reply #4 on: June 05, 2016, 11:24:19 pm »
Yes you are right Teseo
Code: [Select]
osc_add_hook('hook_name', '$this->function_name'); cannot work, I just thought there could be a way I could run the hooks within the class themselves. But I case for now,running  hooks within class objects is not supported by Osclass. So I will just have to use them as you suggest, it works fine though. Thanks so much.

_CONEJO

  • Administrator
  • Hero Member
  • *****
  • Posts: 4689
Re: Using hooks within class objects
« Reply #5 on: June 06, 2016, 11:58:24 am »
You could map hooks to class' methods

Code: [Select]
// This is valid
osc_add_hook('hook_name', array('class_name', 'method_name'));

// This is also valid
osc_add_hook('hook_name', 'Class_name::method_name');

// This works too
$my_object = new MyClass();
osc_add_hook('hook_name', array($my_ocject, 'method_name'));


In cases 1  & 2, the method has to be static, as the class is not instantiated, in case 3 method don't have to be static, but the class has to be instantiated, so it will not work with hooks in the core of osclass.

FredrickReuben

  • Newbie
  • *
  • Posts: 24
Re: Using hooks within class objects
« Reply #6 on: June 10, 2016, 12:08:12 am »
Thanks _CONEJO for you answer. Just a clarification, all this happen in index.php file???

_CONEJO

  • Administrator
  • Hero Member
  • *****
  • Posts: 4689
Re: Using hooks within class objects
« Reply #7 on: June 10, 2016, 12:36:36 am »
yes, or in any other loaded file of your plugin

FredrickReuben

  • Newbie
  • *
  • Posts: 24
Re: Using hooks within class objects
« Reply #8 on: June 11, 2016, 11:26:47 pm »
thank you..