-
Notifications
You must be signed in to change notification settings - Fork 0
/
magic-method-call.php
executable file
·75 lines (58 loc) · 1.91 KB
/
magic-method-call.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
<?php
//__call magic method is used to handle calls to inaccessible or undefined methods within an object.
//It is automatically invoked when an object tries to call a method that is not accessible or does not exist.
//__call mathod use for without define a function you called and use that
class Database {
public $dbhost=''; //properties if deleted this not any issue
public $dbname = '';
public $find="";
function __call($method, $arguments) //this is used for without define a function and access an other function and get value sent a not define a function
{
if(strstr($method, 'get_by_')){
$column = str_replace('get_by_', '', $method);
return $this->get_by($column, $arguments[0]);
}
}
function get_by($column, $find) // $column is sent which cloumn filter and $find is the value for get which record
{
$rows= $this->get_data(); //get a array in get_data private function
if(is_array($rows)) // check array
{
foreach($rows as $row)
{
if(isset($row[$column]) && $row[$column] == $find) // check isset value are get or not and check array name value matched sent value
{
return $row; // find value matched then return my array
}
}
}
}
private function get_data()
{
$arr = array(); // difine array
$arr['id']=1; //asign the value in array
$arr['name']='Test';
$arr['age']='12';
$rows[]=$arr;
$arr = array();
$arr['id']=2;
$arr['name']='Raju';
$arr['age']='15';
$rows[]=$arr;
$arr = array();
$arr['id']=3;
$arr['name']='Amit';
$arr['age']='20';
$rows[]=$arr;
$arr = array();
$arr['id']=4;
$arr['name']='Kapa';
$arr['age']='22';
$rows[]=$arr; // assign all array in one object
return $rows;
}
}
$prod = new Database(); // difine to object class
echo '<pre>';
print_r($prod->get_by_name('Amit')); //get_by_name is not define a function but i have used that beacouse i have use __call method this is magic method
?>