I am not sure what your issue is precisely, you are not creating $post
, just accessing it. It is a very common sight in WordPress.
However from API point of view, is ID of current post is the only thing you need — use get_the_ID()
function to get just that, sans dealing with global yourself.