How to get variable from other function inside class function using add_action for Ajax call

Unless the value of the $sha256 property is hard-coded in the class, you need to store it somewhere (database, file,…) so that you can use it in a different Ajax request.

Once the request to wp_ajax_vault is processed and a response is sent back to the requester all the data in the class properties is lost, if it’s not saved to some persistent place. When your class handles the wp_ajax_retrievedK request, it is happily unaware that it has ever responded to a wp_ajax_vault request.

Within the same request you can naturally update the class properties with new values and then read them in other class methods.

Couple other things to notice

  • make sure there are no unnecessary spaces in the hook names
  • terminate the ajax handler function/method after the processing is done to return a proper response – use e.g. exit(), die(), wp_die(), wp_send_json(), wp_send_json_success() or wp_send_json_error().

Here are couple simplified examples on how you could address these issues.

Example 1, using a class

class MyAjaxHandler
{
    private string $sha256;

    public function __construct(string $hash)
    {
        $this->sha256 = $hash;
    }

    public function run()
    {
        add_action( 'wp_ajax_vault', array( $this, 'vault' ) ); 
        add_action( 'wp_ajax_nopriv_vault', array( $this, 'vault' ) );

        add_action( 'wp_ajax_retrievedK', array( $this, 'retrievedK' ) ); 
        add_action( 'wp_ajax_nopriv_retrievedK', array( $this , 'retrievedK' ) );
    }

    public function vault()
    {
        // handle authentication, authorization, validation, etc.

        // processing...
        echo json_encode(['data' => $this->sha256]);

        // termination
        wp_die();
    }
    
    public function retrievedK()
    {
        // handle authentication, authorization, validation, etc.

        // processing...
        echo json_encode(['data' => $this->sha256]);

        // termination
        wp_die();
    }
}

add_action( 'plugins_loaded', 'my_prefix_init_handler' );
function my_prefix_init_handler() {
    $hash="foo"; // hard-coded, read from a file, retrieve from a databse...
    
    $handler = new MyAjaxHandler($hash);

    $handler->run();
}

UPDATE 2.6.2023

function genk_handler() {
    $hash="thekeygenerated";

    $cryptograph = new Cryptograph(
        // some parameters the class needs...
    );
    
    $handler = new ajaxgenk_handler($hash, new Cryptograph);

    $handler->run();
}

class ajaxgenk_handler
{
    private string $hash;
    private Cryptograph $cryptograph;

    public function __construct(string $hash, Cryptograph $cryptograph)
    {
        $this->hash = $hash;
        $this->cryptograph = $cryptograph;
    }

    public function vault()
    {
        $secureData = $this->cryptograph->generate(); // pseudo method, edit as needed
        
        echo json_encode($secureData);

        wp_die();
    }
}

Example 2, using namespace

namespace My\Plugin;

add_action( 'wp_ajax_vault', __NAMESPACE__ . '\\vault' ); 
add_action( 'wp_ajax_nopriv_vault', __NAMESPACE__ . '\\vault' );

add_action( 'wp_ajax_retrievedK', __NAMESPACE__ . '\\retrievedK' )); 
add_action( 'wp_ajax_nopriv_retrievedK', __NAMESPACE__ . '\\retrievedK'));

function vault() {
    // handle authentication, authorization, validation, etc.

    // processing...
    $sha256 = get_my_sha256();

    // response
    wp_send_json_success(['data' => 'acme']);
}

function retrievedK() {
    // handle authentication, authorization, validation, etc.

    // processing...
    $sha256 = get_my_sha256();

    // response
    wp_send_json_success(['data' => 'acme']);
}

function get_my_sha256() {
    return get_transient(my_transient_name());
}

function save_my_sha256($value) {
    set_transient(my_transient_name(), $value, HOUR_IN_SECONDS);
}

function my_transient_name() {
    return 'foo';
}