Being a morphism of quasiprojective varieties is a local property

Let $X,Y$ be quasiprojective varieties and $\phi \colon X \to Y$ be a map. Suppose there exists a cover $\mathscr U = \{U_i\}_{i \in I}$ of open subsets $U_i \subset X$ such that for every $i \in I$, $\phi \vert_{U_i} \colon U_i \to Y$ is a morphism. Then $\phi \colon X \to Y$ is a morphism.

I have the following definition of morphism: $\psi : X \to Y$ is a morphism if it is a continuous map and for every open set $V \subset Y$ and for every $h\in \mathscr O_Y(V)$ we have $h \circ \psi \in \mathscr O_X(\psi^{-1}(V))$.

Coming back to the exercise, continuity is a local property, hence the first condition is true: if $\phi \vert_{U_i} \colon U_i \to Y$ is continuous for every $i$ then $\phi \colon X \to Y$ is continuous.

I have to prove that $\phi$ preserves regular functions. Take a open set $V \subset Y$. If there exists $i \in I$ s.t. $\phi^{-1}(V) \subset U_i$ for some there is nothing to prove (EDIT: I am not sure of this... maybe I have to use somehow the inclusion $i \colon U_i \hookrightarrow X$...). Suppose then that $\phi^{-1}(V) \subset U_i \cap U_j$. Consider $h \in \mathscr O_Y(V)$: we have $h \circ \phi \in \mathscr O_{U_i}(\phi^{-1}(V) \cap U_i) \cap \mathscr O_{U_j}(\phi^{-1}(V) \cap U_j)$. How can I conclude? I would like to prove $h \circ \phi \in \mathscr O_X(\phi^{-1}(V))$ but I can't see how.