• Hello, I would like to break apart the function used in Class to make it easier to understand, however I cant seem to work out how to pass variables across between two or more functions while maintaining the WP INIT hook. I have simplified my code for this question.

    I am not sure if using multiple add_action in __construct is the correct way to go about it.

    Thanks.

    //called from template/index.php
    do_action('foo');
    
    //in template/fuctions.php
    add_action( 'init', array ( 'foo', 'init' ) );
    
    class foo
    {
    
     public $stillnotworking;
    
        public static function init()
        {
            new self;
        }
    
        public function __construct()
        {
            add_action( 'foo', array ( $this, 'part1' ) );
            add_action( 'foo', array ( $this, 'part2' ) );
        }
    
        public function part1()
        {
          $this->x = '123';
          $stillnotworking = '123';
        }
    
        public function part2()
        {
          if($this->x){
            echo $this->x; //not working
            echo $stillnotworking;
          }
        }
    
        function __destruct() {
    
        }
    
    }

The topic ‘Help with mutiple functions in PHP Class init’ is closed to new replies.