Skip to main content
bold for better visibility
Source Link
jsanc623
  • 2.8k
  • 15
  • 22

I have to create some APIs for a online game and I decided to learn to use namespaces correctly.

I used the factory pattern, then the loadModule method checks if in the Modules folder there is a class named $module and then it return the object. The folders are synchronized with the namespaces.

The main class Metin2.class.phpThe main class Metin2.class.php:

<?php

namespace Metin2;
use mysqli;
use \Metin2\Utilities as Utilities;
require_once("config.php");
require_once(UTILITIES_PATH . "Autoloader.class.php");

class Metin2
{
    public $db;

    public function __construct(mysqli $database)
    {
        spl_autoload_register("Metin2\\Utilities\\Autoloader::loadModel");
        try {
            if(!$database) throw new Exceptions\Metin2Exception("The database instance     is not a valid instance");
            $this->db = new Utilities\Database($database);

        } catch (Exceptions\Metin2Exception $e) {
            echo $e->errorMessage();
        }

    }

    public function loadModule($module)
    {
        $m = "\\Metin2\\Modules\\" . $module;
        return new $m;
    }
}

Autoloader classAutoloader class:

<?php
namespace Metin2\Utilities;


class Autoloader {
    public static function loadModel($class){
        $class = str_replace("\\","/",$class);
        $class = explode("/",$class);
        if($class[0] == "Metin2")unset($class[0]);
        $class = implode("/",$class);
        $path = ROOT_PATH.$class.".class.php";
        if(file_exists($path)){
            require_once($path);
        }
    }
}

An example of a moduleAn example of a module:

<?php
namespace Metin2\Modules;

class Testdb {

    public $db;
    public function __construct(){
        $this->db = \Metin2\Utilities\Database::getInstance();
        print_r($this->db->getOne("account"));
    }
}

Everything works well, but I want to know if there are any things I did wrong or if there are things I can do better.

I have to create some APIs for a online game and I decided to learn to use namespaces correctly.

I used the factory pattern, then the loadModule method checks if in the Modules folder there is a class named $module and then it return the object. The folders are synchronized with the namespaces.

The main class Metin2.class.php:

<?php

namespace Metin2;
use mysqli;
use \Metin2\Utilities as Utilities;
require_once("config.php");
require_once(UTILITIES_PATH . "Autoloader.class.php");

class Metin2
{
    public $db;

    public function __construct(mysqli $database)
    {
        spl_autoload_register("Metin2\\Utilities\\Autoloader::loadModel");
        try {
            if(!$database) throw new Exceptions\Metin2Exception("The database instance     is not a valid instance");
            $this->db = new Utilities\Database($database);

        } catch (Exceptions\Metin2Exception $e) {
            echo $e->errorMessage();
        }

    }

    public function loadModule($module)
    {
        $m = "\\Metin2\\Modules\\" . $module;
        return new $m;
    }
}

Autoloader class:

<?php
namespace Metin2\Utilities;


class Autoloader {
    public static function loadModel($class){
        $class = str_replace("\\","/",$class);
        $class = explode("/",$class);
        if($class[0] == "Metin2")unset($class[0]);
        $class = implode("/",$class);
        $path = ROOT_PATH.$class.".class.php";
        if(file_exists($path)){
            require_once($path);
        }
    }
}

An example of a module:

<?php
namespace Metin2\Modules;

class Testdb {

    public $db;
    public function __construct(){
        $this->db = \Metin2\Utilities\Database::getInstance();
        print_r($this->db->getOne("account"));
    }
}

Everything works well, but I want to know if there are any things I did wrong or if there are things I can do better.

I have to create some APIs for a online game and I decided to learn to use namespaces correctly.

I used the factory pattern, then the loadModule method checks if in the Modules folder there is a class named $module and then it return the object. The folders are synchronized with the namespaces.

The main class Metin2.class.php:

<?php

namespace Metin2;
use mysqli;
use \Metin2\Utilities as Utilities;
require_once("config.php");
require_once(UTILITIES_PATH . "Autoloader.class.php");

class Metin2
{
    public $db;

    public function __construct(mysqli $database)
    {
        spl_autoload_register("Metin2\\Utilities\\Autoloader::loadModel");
        try {
            if(!$database) throw new Exceptions\Metin2Exception("The database instance     is not a valid instance");
            $this->db = new Utilities\Database($database);

        } catch (Exceptions\Metin2Exception $e) {
            echo $e->errorMessage();
        }

    }

    public function loadModule($module)
    {
        $m = "\\Metin2\\Modules\\" . $module;
        return new $m;
    }
}

Autoloader class:

<?php
namespace Metin2\Utilities;


class Autoloader {
    public static function loadModel($class){
        $class = str_replace("\\","/",$class);
        $class = explode("/",$class);
        if($class[0] == "Metin2")unset($class[0]);
        $class = implode("/",$class);
        $path = ROOT_PATH.$class.".class.php";
        if(file_exists($path)){
            require_once($path);
        }
    }
}

An example of a module:

<?php
namespace Metin2\Modules;

class Testdb {

    public $db;
    public function __construct(){
        $this->db = \Metin2\Utilities\Database::getInstance();
        print_r($this->db->getOne("account"));
    }
}

Everything works well, but I want to know if there are any things I did wrong or if there are things I can do better.

Please don't add "thanks" - show them with upvotes
Source Link
Jamal
  • 34.9k
  • 13
  • 133
  • 237

PHP OOP my My first project using namespaces

iI have to create some APIs for a online game and iI decided to learn to use namespaces correctly.

I used the factory pattern, then the loadModuleloadModule method checks if in the Modules folder there is a class named $module and then it return the object. The folders are synchronized with the namespaces.

The main class Metin2.class.php:

<?php

namespace Metin2;
use mysqli;
use \Metin2\Utilities as Utilities;
require_once("config.php");
require_once(UTILITIES_PATH . "Autoloader.class.php");

class Metin2
{
    public $db;

    public function __construct(mysqli $database)
    {
        spl_autoload_register("Metin2\\Utilities\\Autoloader::loadModel");
        try {
            if(!$database) throw new Exceptions\Metin2Exception("The database instance     is not a valid instance");
            $this->db = new Utilities\Database($database);

        } catch (Exceptions\Metin2Exception $e) {
            echo $e->errorMessage();
        }

    }

    public function loadModule($module)
    {
        $m = "\\Metin2\\Modules\\" . $module;
        return new $m;
    }
}

Autoloader class:

<?php
namespace Metin2\Utilities;


class Autoloader {
    public static function loadModel($class){
        $class = str_replace("\\","/",$class);
        $class = explode("/",$class);
        if($class[0] == "Metin2")unset($class[0]);
        $class = implode("/",$class);
        $path = ROOT_PATH.$class.".class.php";
        if(file_exists($path)){
            require_once($path);
        }
    }
}

An example of a module:

<?php
namespace Metin2\Modules;

class Testdb {

    public $db;
    public function __construct(){
        $this->db = \Metin2\Utilities\Database::getInstance();
        print_r($this->db->getOne("account"));
    }
}

Everything works well, but my question isI want to know if there are any things iI did wrong or if there are things iI can do better.

Thanks :D

PHP OOP my first project using namespaces

i have to create some APIs for a online game and i decided to learn to use namespaces correctly.

I used the factory pattern, then the loadModule method checks if in the Modules folder there is a class named $module and then it return the object. The folders are synchronized with the namespaces.

The main class Metin2.class.php:

<?php

namespace Metin2;
use mysqli;
use \Metin2\Utilities as Utilities;
require_once("config.php");
require_once(UTILITIES_PATH . "Autoloader.class.php");

class Metin2
{
    public $db;

    public function __construct(mysqli $database)
    {
        spl_autoload_register("Metin2\\Utilities\\Autoloader::loadModel");
        try {
            if(!$database) throw new Exceptions\Metin2Exception("The database instance     is not a valid instance");
            $this->db = new Utilities\Database($database);

        } catch (Exceptions\Metin2Exception $e) {
            echo $e->errorMessage();
        }

    }

    public function loadModule($module)
    {
        $m = "\\Metin2\\Modules\\" . $module;
        return new $m;
    }
}

Autoloader class:

<?php
namespace Metin2\Utilities;


class Autoloader {
    public static function loadModel($class){
        $class = str_replace("\\","/",$class);
        $class = explode("/",$class);
        if($class[0] == "Metin2")unset($class[0]);
        $class = implode("/",$class);
        $path = ROOT_PATH.$class.".class.php";
        if(file_exists($path)){
            require_once($path);
        }
    }
}

An example of a module:

<?php
namespace Metin2\Modules;

class Testdb {

    public $db;
    public function __construct(){
        $this->db = \Metin2\Utilities\Database::getInstance();
        print_r($this->db->getOne("account"));
    }
}

Everything works well, but my question is if there are any things i did wrong or if there are things i can do better.

Thanks :D

My first project using namespaces

I have to create some APIs for a online game and I decided to learn to use namespaces correctly.

I used the factory pattern, then the loadModule method checks if in the Modules folder there is a class named $module and then it return the object. The folders are synchronized with the namespaces.

The main class Metin2.class.php:

<?php

namespace Metin2;
use mysqli;
use \Metin2\Utilities as Utilities;
require_once("config.php");
require_once(UTILITIES_PATH . "Autoloader.class.php");

class Metin2
{
    public $db;

    public function __construct(mysqli $database)
    {
        spl_autoload_register("Metin2\\Utilities\\Autoloader::loadModel");
        try {
            if(!$database) throw new Exceptions\Metin2Exception("The database instance     is not a valid instance");
            $this->db = new Utilities\Database($database);

        } catch (Exceptions\Metin2Exception $e) {
            echo $e->errorMessage();
        }

    }

    public function loadModule($module)
    {
        $m = "\\Metin2\\Modules\\" . $module;
        return new $m;
    }
}

Autoloader class:

<?php
namespace Metin2\Utilities;


class Autoloader {
    public static function loadModel($class){
        $class = str_replace("\\","/",$class);
        $class = explode("/",$class);
        if($class[0] == "Metin2")unset($class[0]);
        $class = implode("/",$class);
        $path = ROOT_PATH.$class.".class.php";
        if(file_exists($path)){
            require_once($path);
        }
    }
}

An example of a module:

<?php
namespace Metin2\Modules;

class Testdb {

    public $db;
    public function __construct(){
        $this->db = \Metin2\Utilities\Database::getInstance();
        print_r($this->db->getOne("account"));
    }
}

Everything works well, but I want to know if there are any things I did wrong or if there are things I can do better.

Source Link

PHP OOP my first project using namespaces

i have to create some APIs for a online game and i decided to learn to use namespaces correctly.

I used the factory pattern, then the loadModule method checks if in the Modules folder there is a class named $module and then it return the object. The folders are synchronized with the namespaces.

The main class Metin2.class.php:

<?php

namespace Metin2;
use mysqli;
use \Metin2\Utilities as Utilities;
require_once("config.php");
require_once(UTILITIES_PATH . "Autoloader.class.php");

class Metin2
{
    public $db;

    public function __construct(mysqli $database)
    {
        spl_autoload_register("Metin2\\Utilities\\Autoloader::loadModel");
        try {
            if(!$database) throw new Exceptions\Metin2Exception("The database instance     is not a valid instance");
            $this->db = new Utilities\Database($database);

        } catch (Exceptions\Metin2Exception $e) {
            echo $e->errorMessage();
        }

    }

    public function loadModule($module)
    {
        $m = "\\Metin2\\Modules\\" . $module;
        return new $m;
    }
}

Autoloader class:

<?php
namespace Metin2\Utilities;


class Autoloader {
    public static function loadModel($class){
        $class = str_replace("\\","/",$class);
        $class = explode("/",$class);
        if($class[0] == "Metin2")unset($class[0]);
        $class = implode("/",$class);
        $path = ROOT_PATH.$class.".class.php";
        if(file_exists($path)){
            require_once($path);
        }
    }
}

An example of a module:

<?php
namespace Metin2\Modules;

class Testdb {

    public $db;
    public function __construct(){
        $this->db = \Metin2\Utilities\Database::getInstance();
        print_r($this->db->getOne("account"));
    }
}

Everything works well, but my question is if there are any things i did wrong or if there are things i can do better.

Thanks :D