[{"data":1,"prerenderedAt":644},["ShallowReactive",2],{"content-all-\u002Fphp\u002Fpatterns":3,"sidebar-content":532,"all-tags":592},[4],{"_path":5,"_dir":6,"_draft":7,"_partial":7,"_locale":8,"title":9,"description":10,"tags":11,"date":14,"body":15,"_type":526,"_id":527,"_source":528,"_file":529,"_stem":530,"_extension":531},"\u002Fphp\u002Fpatterns\u002Fsingleton","patterns",false,"","Singleton Pattern","Implementing the singleton pattern in PHP",[12,6,13],"php","backend","2026-03-13",{"type":16,"children":17,"toc":523},"root",[18,26,32,517],{"type":19,"tag":20,"props":21,"children":23},"element","h2",{"id":22},"singleton-pattern",[24],{"type":25,"value":9},"text",{"type":19,"tag":27,"props":28,"children":29},"p",{},[30],{"type":25,"value":31},"The singleton pattern ensures a class has only one instance.",{"type":19,"tag":33,"props":34,"children":37},"pre",{"className":35,"code":36,"language":12,"meta":8,"style":8},"language-php shiki shiki-themes github-light github-dark","class Database\n{\n    private static ?self $instance = null;\n\n    private function __construct(\n        private readonly PDO $pdo\n    ) {}\n\n    public static function getInstance(): self\n    {\n        if (self::$instance === null) {\n            self::$instance = new self(\n                new PDO('mysql:host=localhost;dbname=app', 'root', '')\n            );\n        }\n        return self::$instance;\n    }\n\n    public function query(string $sql): array\n    {\n        return $this->pdo->query($sql)->fetchAll();\n    }\n}\n",[38],{"type":19,"tag":39,"props":40,"children":41},"code",{"__ignoreMap":8},[42,60,70,110,120,143,167,176,184,221,230,268,299,347,356,365,384,393,401,441,449,500,508],{"type":19,"tag":43,"props":44,"children":47},"span",{"class":45,"line":46},"line",1,[48,54],{"type":19,"tag":43,"props":49,"children":51},{"style":50},"--shiki-default:#D73A49;--shiki-dark:#F97583",[52],{"type":25,"value":53},"class",{"type":19,"tag":43,"props":55,"children":57},{"style":56},"--shiki-default:#6F42C1;--shiki-dark:#B392F0",[58],{"type":25,"value":59}," Database\n",{"type":19,"tag":43,"props":61,"children":63},{"class":45,"line":62},2,[64],{"type":19,"tag":43,"props":65,"children":67},{"style":66},"--shiki-default:#24292E;--shiki-dark:#E1E4E8",[68],{"type":25,"value":69},"{\n",{"type":19,"tag":43,"props":71,"children":73},{"class":45,"line":72},3,[74,79,84,89,94,99,105],{"type":19,"tag":43,"props":75,"children":76},{"style":50},[77],{"type":25,"value":78},"    private",{"type":19,"tag":43,"props":80,"children":81},{"style":50},[82],{"type":25,"value":83}," static",{"type":19,"tag":43,"props":85,"children":86},{"style":50},[87],{"type":25,"value":88}," ?self",{"type":19,"tag":43,"props":90,"children":91},{"style":66},[92],{"type":25,"value":93}," $instance ",{"type":19,"tag":43,"props":95,"children":96},{"style":50},[97],{"type":25,"value":98},"=",{"type":19,"tag":43,"props":100,"children":102},{"style":101},"--shiki-default:#005CC5;--shiki-dark:#79B8FF",[103],{"type":25,"value":104}," null",{"type":19,"tag":43,"props":106,"children":107},{"style":66},[108],{"type":25,"value":109},";\n",{"type":19,"tag":43,"props":111,"children":113},{"class":45,"line":112},4,[114],{"type":19,"tag":43,"props":115,"children":117},{"emptyLinePlaceholder":116},true,[118],{"type":25,"value":119},"\n",{"type":19,"tag":43,"props":121,"children":123},{"class":45,"line":122},5,[124,128,133,138],{"type":19,"tag":43,"props":125,"children":126},{"style":50},[127],{"type":25,"value":78},{"type":19,"tag":43,"props":129,"children":130},{"style":50},[131],{"type":25,"value":132}," function",{"type":19,"tag":43,"props":134,"children":135},{"style":101},[136],{"type":25,"value":137}," __construct",{"type":19,"tag":43,"props":139,"children":140},{"style":66},[141],{"type":25,"value":142},"(\n",{"type":19,"tag":43,"props":144,"children":146},{"class":45,"line":145},6,[147,152,157,162],{"type":19,"tag":43,"props":148,"children":149},{"style":50},[150],{"type":25,"value":151},"        private",{"type":19,"tag":43,"props":153,"children":154},{"style":50},[155],{"type":25,"value":156}," readonly",{"type":19,"tag":43,"props":158,"children":159},{"style":101},[160],{"type":25,"value":161}," PDO",{"type":19,"tag":43,"props":163,"children":164},{"style":66},[165],{"type":25,"value":166}," $pdo\n",{"type":19,"tag":43,"props":168,"children":170},{"class":45,"line":169},7,[171],{"type":19,"tag":43,"props":172,"children":173},{"style":66},[174],{"type":25,"value":175},"    ) {}\n",{"type":19,"tag":43,"props":177,"children":179},{"class":45,"line":178},8,[180],{"type":19,"tag":43,"props":181,"children":182},{"emptyLinePlaceholder":116},[183],{"type":25,"value":119},{"type":19,"tag":43,"props":185,"children":187},{"class":45,"line":186},9,[188,193,197,201,206,211,216],{"type":19,"tag":43,"props":189,"children":190},{"style":50},[191],{"type":25,"value":192},"    public",{"type":19,"tag":43,"props":194,"children":195},{"style":50},[196],{"type":25,"value":83},{"type":19,"tag":43,"props":198,"children":199},{"style":50},[200],{"type":25,"value":132},{"type":19,"tag":43,"props":202,"children":203},{"style":56},[204],{"type":25,"value":205}," getInstance",{"type":19,"tag":43,"props":207,"children":208},{"style":66},[209],{"type":25,"value":210},"()",{"type":19,"tag":43,"props":212,"children":213},{"style":50},[214],{"type":25,"value":215},":",{"type":19,"tag":43,"props":217,"children":218},{"style":50},[219],{"type":25,"value":220}," self\n",{"type":19,"tag":43,"props":222,"children":224},{"class":45,"line":223},10,[225],{"type":19,"tag":43,"props":226,"children":227},{"style":66},[228],{"type":25,"value":229},"    {\n",{"type":19,"tag":43,"props":231,"children":233},{"class":45,"line":232},11,[234,239,244,249,254,259,263],{"type":19,"tag":43,"props":235,"children":236},{"style":50},[237],{"type":25,"value":238},"        if",{"type":19,"tag":43,"props":240,"children":241},{"style":66},[242],{"type":25,"value":243}," (",{"type":19,"tag":43,"props":245,"children":246},{"style":50},[247],{"type":25,"value":248},"self::",{"type":19,"tag":43,"props":250,"children":251},{"style":66},[252],{"type":25,"value":253},"$instance ",{"type":19,"tag":43,"props":255,"children":256},{"style":50},[257],{"type":25,"value":258},"===",{"type":19,"tag":43,"props":260,"children":261},{"style":101},[262],{"type":25,"value":104},{"type":19,"tag":43,"props":264,"children":265},{"style":66},[266],{"type":25,"value":267},") {\n",{"type":19,"tag":43,"props":269,"children":271},{"class":45,"line":270},12,[272,277,281,285,290,295],{"type":19,"tag":43,"props":273,"children":274},{"style":50},[275],{"type":25,"value":276},"            self::",{"type":19,"tag":43,"props":278,"children":279},{"style":66},[280],{"type":25,"value":253},{"type":19,"tag":43,"props":282,"children":283},{"style":50},[284],{"type":25,"value":98},{"type":19,"tag":43,"props":286,"children":287},{"style":50},[288],{"type":25,"value":289}," new",{"type":19,"tag":43,"props":291,"children":292},{"style":50},[293],{"type":25,"value":294}," self",{"type":19,"tag":43,"props":296,"children":297},{"style":66},[298],{"type":25,"value":142},{"type":19,"tag":43,"props":300,"children":302},{"class":45,"line":301},13,[303,308,312,317,323,328,333,337,342],{"type":19,"tag":43,"props":304,"children":305},{"style":50},[306],{"type":25,"value":307},"                new",{"type":19,"tag":43,"props":309,"children":310},{"style":101},[311],{"type":25,"value":161},{"type":19,"tag":43,"props":313,"children":314},{"style":66},[315],{"type":25,"value":316},"(",{"type":19,"tag":43,"props":318,"children":320},{"style":319},"--shiki-default:#032F62;--shiki-dark:#9ECBFF",[321],{"type":25,"value":322},"'mysql:host=localhost;dbname=app'",{"type":19,"tag":43,"props":324,"children":325},{"style":66},[326],{"type":25,"value":327},", ",{"type":19,"tag":43,"props":329,"children":330},{"style":319},[331],{"type":25,"value":332},"'root'",{"type":19,"tag":43,"props":334,"children":335},{"style":66},[336],{"type":25,"value":327},{"type":19,"tag":43,"props":338,"children":339},{"style":319},[340],{"type":25,"value":341},"''",{"type":19,"tag":43,"props":343,"children":344},{"style":66},[345],{"type":25,"value":346},")\n",{"type":19,"tag":43,"props":348,"children":350},{"class":45,"line":349},14,[351],{"type":19,"tag":43,"props":352,"children":353},{"style":66},[354],{"type":25,"value":355},"            );\n",{"type":19,"tag":43,"props":357,"children":359},{"class":45,"line":358},15,[360],{"type":19,"tag":43,"props":361,"children":362},{"style":66},[363],{"type":25,"value":364},"        }\n",{"type":19,"tag":43,"props":366,"children":368},{"class":45,"line":367},16,[369,374,379],{"type":19,"tag":43,"props":370,"children":371},{"style":50},[372],{"type":25,"value":373},"        return",{"type":19,"tag":43,"props":375,"children":376},{"style":50},[377],{"type":25,"value":378}," self::",{"type":19,"tag":43,"props":380,"children":381},{"style":66},[382],{"type":25,"value":383},"$instance;\n",{"type":19,"tag":43,"props":385,"children":387},{"class":45,"line":386},17,[388],{"type":19,"tag":43,"props":389,"children":390},{"style":66},[391],{"type":25,"value":392},"    }\n",{"type":19,"tag":43,"props":394,"children":396},{"class":45,"line":395},18,[397],{"type":19,"tag":43,"props":398,"children":399},{"emptyLinePlaceholder":116},[400],{"type":25,"value":119},{"type":19,"tag":43,"props":402,"children":404},{"class":45,"line":403},19,[405,409,413,418,422,427,432,436],{"type":19,"tag":43,"props":406,"children":407},{"style":50},[408],{"type":25,"value":192},{"type":19,"tag":43,"props":410,"children":411},{"style":50},[412],{"type":25,"value":132},{"type":19,"tag":43,"props":414,"children":415},{"style":56},[416],{"type":25,"value":417}," query",{"type":19,"tag":43,"props":419,"children":420},{"style":66},[421],{"type":25,"value":316},{"type":19,"tag":43,"props":423,"children":424},{"style":50},[425],{"type":25,"value":426},"string",{"type":19,"tag":43,"props":428,"children":429},{"style":66},[430],{"type":25,"value":431}," $sql)",{"type":19,"tag":43,"props":433,"children":434},{"style":50},[435],{"type":25,"value":215},{"type":19,"tag":43,"props":437,"children":438},{"style":50},[439],{"type":25,"value":440}," array\n",{"type":19,"tag":43,"props":442,"children":444},{"class":45,"line":443},20,[445],{"type":19,"tag":43,"props":446,"children":447},{"style":66},[448],{"type":25,"value":229},{"type":19,"tag":43,"props":450,"children":452},{"class":45,"line":451},21,[453,457,462,467,472,476,481,486,490,495],{"type":19,"tag":43,"props":454,"children":455},{"style":50},[456],{"type":25,"value":373},{"type":19,"tag":43,"props":458,"children":459},{"style":101},[460],{"type":25,"value":461}," $this",{"type":19,"tag":43,"props":463,"children":464},{"style":50},[465],{"type":25,"value":466},"->",{"type":19,"tag":43,"props":468,"children":469},{"style":66},[470],{"type":25,"value":471},"pdo",{"type":19,"tag":43,"props":473,"children":474},{"style":50},[475],{"type":25,"value":466},{"type":19,"tag":43,"props":477,"children":478},{"style":56},[479],{"type":25,"value":480},"query",{"type":19,"tag":43,"props":482,"children":483},{"style":66},[484],{"type":25,"value":485},"($sql)",{"type":19,"tag":43,"props":487,"children":488},{"style":50},[489],{"type":25,"value":466},{"type":19,"tag":43,"props":491,"children":492},{"style":56},[493],{"type":25,"value":494},"fetchAll",{"type":19,"tag":43,"props":496,"children":497},{"style":66},[498],{"type":25,"value":499},"();\n",{"type":19,"tag":43,"props":501,"children":503},{"class":45,"line":502},22,[504],{"type":19,"tag":43,"props":505,"children":506},{"style":66},[507],{"type":25,"value":392},{"type":19,"tag":43,"props":509,"children":511},{"class":45,"line":510},23,[512],{"type":19,"tag":43,"props":513,"children":514},{"style":66},[515],{"type":25,"value":516},"}\n",{"type":19,"tag":518,"props":519,"children":520},"style",{},[521],{"type":25,"value":522},"html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html.dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}",{"title":8,"searchDepth":72,"depth":72,"links":524},[525],{"id":22,"depth":62,"text":9},"markdown","content:php:patterns:singleton.md","content","php\u002Fpatterns\u002Fsingleton.md","php\u002Fpatterns\u002Fsingleton","md",[533,536,539,542,545,548,551,554,557,560,564,567,570,573,576,579,582,585,588,589],{"_path":534,"title":535,"weight":223},"\u002Fdata-structure-algorithm\u002Farray-manipulation\u002Ffor-looping","For Looping in Python",{"_path":537,"title":538,"weight":223},"\u002Fdata-structure-algorithm\u002Farray-manipulation\u002Fwhile-looping","While Looping in Python",{"_path":540,"title":541,"weight":72},"\u002Fdata-structure-algorithm\u002Fassignments","Assignments",{"_path":543,"title":544,"weight":46},"\u002Fdata-structure-algorithm\u002Fbuzzword","Buzzword List DSA",{"_path":546,"title":547,"weight":62},"\u002Fdata-structure-algorithm\u002Fdata-type","Data Types",{"_path":549,"title":550,"weight":223},"\u002Fdata-structure-algorithm\u002Fhash-map","Hash Map",{"_path":552,"title":553,"weight":46},"\u002Fdata-structure-algorithm\u002Finput-output","Input and Output",{"_path":555,"title":556,"weight":443},"\u002Fdata-structure-algorithm\u002Fmath-library\u002Fsum","Sum Elements",{"_path":558,"title":559,"weight":223},"\u002Fdata-structure-algorithm\u002Fmath-library\u002Fsummary","Summary",{"_path":561,"title":562,"weight":563},"\u002Fdocker\u002Fcheat-sheets","Docker Cheat Sheets",100,{"_path":565,"title":566,"weight":443},"\u002Fdocker\u002Fdocker-dasar\u002Fdocker-container","Docker Container",{"_path":568,"title":569,"weight":502},"\u002Fdocker\u002Fdocker-dasar\u002Fdocker-execution","Docker Execution",{"_path":571,"title":572,"weight":451},"\u002Fdocker\u002Fdocker-dasar\u002Fdocker-image","Docker Image",{"_path":574,"title":575,"weight":510},"\u002Fdocker\u002Fdocker-dasar\u002Fdocker-logging","Docker Logging",{"_path":577,"title":578,"weight":223},"\u002Fdocker\u002Fdocker-prerequisites\u002Fdocker-installation","Docker Installation",{"_path":580,"title":581,"weight":46},"\u002Fdocker\u002Fresources","Docker Resources",{"_path":583,"title":584},"\u002Fjavascript\u002Fpromises","Promise Patterns",{"_path":586,"title":587,"weight":46},"\u002Fphp\u002Farrays","PHP Array Functions",{"_path":5,"title":9},{"_path":590,"title":591,"weight":62},"\u002Fphp\u002Freferences","PHP References (&)",[593,597,599,601,606,608,610,612,614,616,621,623,625,627,629,631,633,638,640,642],{"tags":594},[595,13,596],"python","arrays",{"tags":598},[595,13,596],{"tags":600},[595,13],{"tags":602},[603,604,605],"dsa","algorithms","data-structures",{"tags":607},[595,13],{"tags":609},[595,13],{"tags":611},[595,13],{"tags":613},[595,13,596],{"tags":615},[595,13,596],{"tags":617},[618,619,620],"docker","devops","infrastructure",{"tags":622},[618,619,620],{"tags":624},[618,619,620],{"tags":626},[618,619,620],{"tags":628},[618,619,620],{"tags":630},[618,619,620],{"tags":632},[618,619,620],{"tags":634},[635,636,637],"javascript","frontend","async",{"tags":639},[12,13,596],{"tags":641},[12,6,13],{"tags":643},[12,13],1777825384580]