Grab ID from post from plugin and use it to create an object

I have this code within a plugin and I want to grab the ID of the
custom post being showed:

“here” is in the same file that classes are created in the main file
of the plugin:

class Product {
}

$x = new Product($id); //here

You can’t do that. The “custom post being showed” isn’t being showed on your plugin page, and your action won’t fire on the backend at all. You will have to create your object inside the wp_head hook or some other front-end hook — something like:

function getPostId(){ 
    global $wp_query;
    global $x; // bad variable name
    $x = new Product($wp_query->post->ID);
} 
add_action('wp_head','getPostId'); 

It is very possible that you could rewrite the class to avoid some or all of that global mess.

If you need the ID of a page on the backend you are going to have to find another way to identify the page. You simply can’t hook to an an action that doesn’t fire to find the ID of a page that isn’t loading.