Method overload in php?

    I learned to program first in Java, then the work of a PHP programmer turned up and I quickly moved to it. However, as far as you can know, Java is a strongly typed language, and PHP is not, from here I had some problems writing some classes.
    For example, in PHP there is no explicit method overload, I suggest discussing how to find a way out of such a situation.

    For example, consider a class of products in an online store.
    class Prodcuts {
                public $ name;
    	public $ price;
    	public $ id;
    	function __construct ($ id) {
    		$ sql = "SELECT * FROM products WHERE id = $ id";
    		$ result = mysql_query ($ sql);
    		$ array = mysql_fetch_array ($ result);
    		$ this-> name = $ array ['name']; 
    		$ this-> price = $ array ['price'];
    		$ this-> id = $ id; 
    	}
    }
    



    It will be possible to create a new object in this case:
    $ objProd = new Product (1)
    In the class constructor, everything related to id = 1 is selected and the object is initialized.
    But at some point I realized I need to initialize the
    object according to other parameters, for example for such a method:
    	static function create ($ name, $ price) {
    		$ sql = "INSERT INTO poducts (name, price) VALUES ('$ name', '$ price')";
    		if (mysql_query ($ sql)) {
    			return new Prodcuts ($ name, $ price);
    		}
    	}
    



    Here the question of constructor overload or some alternative way of initializing an object was born.
    I know several ways and share them with swamis. I would be glad if someone offers more options for solving this problem.

    Method number 1. Using optional parameters.
    Redo the constructor like this:

    	function __construct ($ id = null, $ name = null, $ price = null) {
    		if (isset ($ name) && isset ($ price)) {
    			$ this-> name = $ name;
    			$ this-> price = $ price;
    		} else {
    			$ sql = "SELECT * FROM products WHERE id = $ id";
    			$ result = mysql_query ($ sql);
    			$ array = mysql_fetch_array ($ result);
    			$ this-> name = $ array ['name']; 
    			$ this-> price = $ array ['price'];
    			$ this-> id = $ id; 
    		}
    	}
    


    that is, we can omit the id parameter
    as follows:
    $ product = new Prodcuts (null, $ name, $ price);
    agree, this is not very beautiful.

    Method number 2. Using the func_num_args () and func_get_arg () functions.


    	function __construct ($ id) {
    		if (func_num_args ()> 1) {
    			$ this-> name = func_get_arg (0);
    			$ this-> price = func_get_arg (1);
    		} else {
    			$ sql = "SELECT * FROM products WHERE id = $ id";
    			$ result = mysql_query ($ sql);
    			$ array = mysql_fetch_array ($ result);
    			$ this-> name = $ array ['name']; 
    			$ this-> price = $ array ['price'];
    			$ this-> id = $ id; 
    		}
    	}
    


    Here we use two "magic" functions:
    func_num_args () - returns the number of arguments of the current function,
    func_get_arg () - returns the specified argument of the current function (counting from zero),
    check the number of specified parameters and initialize the object as we need.

    Method number 3. Use for initialization of a static function rejection of "overload" of constructors

    static public function createWithTwoParams ($ name, $ price) { 
        $ object = new self ();
        $ object-> initTwoParams ($ name, $ price);
        return $ object;
    }
    protected function initTwoParams ($ price, $ name) {
    	$ this-> name = $ name;
    	$ this-> price = $ price;
    }
    



    In this case, it turns out: a
    static function can be executed without creating an object, and it will return an instance of the class product
    $ product = Products :: createWithTwoParams ($ name, $ price);
    But we can also use the constructor.
    $ product = new Prodcuts ($ id);

    Although PHP does not have overload and something else, but non-strict typing allows you to do what “adult languages” never dreamed of, and besides, it’s very convenient, and all problems with a lack of something can be solved.

    PS: my first post, do not judge strictly.

    Also popular now: