Home > Backend Development > PHP Tutorial > laravel singleton class that connects multiple different databases

laravel singleton class that connects multiple different databases

PHP中文网
Release: 2023-03-11 09:20:02
Original
1229 people have browsed it

When connecting to multiple different databases, you need to write multiple connections. In order to simplify the operation, you can use this base class. As long as different databases establish corresponding classes and inherit this class, you can use the ORM model to operate. .

<code class="sourceCode php"><span class="kw">class</span> singletonInstance 
{

    <span class="kw">protected</span> <span class="kw">static</span> <span class="kw">$instances</span> = <span class="ot">[];</span>
    <span class="kw">protected</span> <span class="kw">static</span> <span class="kw">$table</span> = <span class="kw">null</span><span class="ot">;</span>
    <span class="kw">protected</span> <span class="kw">static</span> <span class="kw">$connection</span> = <span class="kw">null</span><span class="ot">;</span>

    <span class="kw">public</span> <span class="kw">function</span> <span class="fu">__construct</span><span class="ot">()</span>
    {
        <span class="kw">if</span><span class="ot">(</span>!<span class="fu">isset</span><span class="ot">(</span><span class="kw">static</span>::<span class="kw">$instances</span><span class="ot">[</span><span class="kw">static</span>::<span class="kw">class</span><span class="ot">]))</span>{
            <span class="kw">if</span><span class="ot">(</span><span class="fu">is_null</span><span class="ot">(</span><span class="kw">self</span>::<span class="kw">$table</span><span class="ot">)</span> || <span class="fu">is_null</span><span class="ot">(</span><span class="kw">self</span>::<span class="kw">$connection</span><span class="ot">))</span>
            {
                <span class="kw">throw</span> <span class="kw">new</span> \<span class="kw">Exception</span><span class="ot">(</span><span class="st">'not defined property'</span><span class="ot">);</span>
            }
            <span class="kw">static</span>::<span class="kw">$instances</span><span class="ot">[</span><span class="kw">static</span>::<span class="kw">class</span><span class="ot">]</span> = <span class="kw">DB</span>::connection<span class="ot">(</span><span class="kw">self</span>::connection<span class="ot">)</span>->table<span class="ot">(</span><span class="kw">self</span>::<span class="kw">$table</span><span class="ot">);</span>
        }
    }

    <span class="kw">public</span> <span class="kw">static</span> <span class="kw">function</span> getDB<span class="ot">()</span>
    {
        <span class="kw">return</span> <span class="kw">static</span>::<span class="kw">$instances</span><span class="ot">[</span><span class="kw">static</span>::<span class="kw">class</span><span class="ot">];</span>
    }

}</code>
Copy after login

The above is the detailed content of laravel singleton class that connects multiple different databases. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template