How to modify a global variable in a function and use it on another function?

The way I’d do it is to not use global variables.

This is a simple class that uses 3 methods and 1 property to hook into the_post and echo the post ID in the footer.

/**
 * Plugin Name: WPSE_263293 Example
 */

class wpse_263293 {
  protected $ID;

  //* Add actions on init
  public function init() {
    add_action( 'the_post', [ $this, 'the_post' ] );
    add_action( 'wp_footer', [ $this, 'footer' ] );
  }
  public function the_post( $post ) {
    //* Only do this once
    remove_action( 'the_post', [ $this, 'the_post' ] );

    //* This is the property we're interested in
    $this->ID = $post->ID;
  }
  public function footer() {
    //* Print the ID property in the footer
    echo $this->ID;
  }
}
//* Initiate the class and hook into init
add_action( 'init', [ $wpse_263293 = new wpse_263293(), 'init' ] );