-
Notifications
You must be signed in to change notification settings - Fork 0
/
magic-method-getter.php
executable file
·66 lines (51 loc) · 1.81 KB
/
magic-method-getter.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
<?php
//__get is a magic method that is automatically called when accessing inaccessible or undefined properties of an object.
//It allows you to define custom behavior when a property is being accessed or retrieved. one more think __get used for limited data
class Database {
public $dbhost=''; //properties if deleted this not any issue
public $dbname = '';
public $find="";
function __get($name) // this used basicly for set properties & value $name used for get your properties(passed field name) and $value for set value
{
$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[$name]) && $row[$name] == $this->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
$prod->find="Amit"; //$prod->find use for search any value if you want to get any the search age and id your difine
echo '<pre>';
print_r($prod->name); //$prod->name is useing for same perameter name is __get function(magic method) if want to get name properties value so use get and if you want to search any other field value so chnage field name
?>