I learned how to program first in Java, then turned up the work of a PHP programmer and I quickly moved to it. However, as far as you can know Java is a strong typing language, but PHP is not, from here I have some problems with writing some classes.
For example, in PHP there is no explicit method overload, I propose to discuss how to find a way out of such a suturation.
For example, consider the class of products allowed in the 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;
}
}
To create a new object in such a case it will be possible to:
$ 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 needed to initialize the object and other parameters,
For this 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 was born the question of overloading the constructor or some alternative method of initializing the object.
I know several ways and share them with swami. I would be glad if someone offers more solutions to this problem.
')
Method number 1. Using optional parameters.To redo constructor so:
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;
}
}
Ie we can not specify the id parameter
it turns out like this:
$ product = new Prodcuts (null, $ name, $ price);
agree, it 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 starts from zero),
check the number of the specified parameters and initialize the object as we need.
Method number 3. Use for initialization of the static function failure of the "overload" of designers
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 performed without creating an object, and it will return an instance of the product class
$ product = Products :: createWithTwoParams ($ name, $ price);
But we can use the constructor too.
$ product = new Prodcuts ($ id);
Suppose there is no overload in PHP and something else, but a gentle typing allows you to get up to something that “adult languages” never dreamed of, and besides it is very convenient, and all the problems with knitting with the lack of something can be solved.
PS: my first post, do not judge strictly.