Skip to content

Commit f37314b

Browse files
author
navjotsinghprince
committed
The PHP Professional
1 parent 7f54266 commit f37314b

File tree

5 files changed

+105
-0
lines changed

5 files changed

+105
-0
lines changed
File renamed without changes.
File renamed without changes.
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
//If we attempt to access any private property or a non-existing property, this method is automatically invoked.
3+
4+
5+
#First Example----------------------
6+
/*
7+
class abc{
8+
private $name="Navjot Singh";
9+
10+
public function __get($PropertyName){
11+
echo "You Are Trying To access Non-Existing Or Private Property($PropertyName)";
12+
}
13+
14+
}
15+
$test= new abc();
16+
$test->name;
17+
*/
18+
19+
20+
class abc
21+
{
22+
private $data = ["name" => "Prince", "course" => "PHP", "fee" => 10000];
23+
24+
public function __get($key)
25+
{
26+
#echo "You Are Trying To access Non-Existing Or Private Property($PropertyName)";
27+
if (array_key_exists($key, $this->data)) {
28+
return $this->data[$key];
29+
} else {
30+
return "This Key($key) is not Defined";
31+
}
32+
}
33+
}
34+
35+
$test = new abc();
36+
//print_r($test->data);
37+
38+
echo $test->name;
39+
echo $test->course;
40+
echo $test->age;
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
//This function is called when we set the value of a private property from outside.
3+
4+
class abc
5+
{
6+
private $name;
7+
8+
public function __get($PropertyName)
9+
{
10+
echo "You Are Trying To access Non-Existing Or Private Property($PropertyName)";
11+
}
12+
13+
public function __set($propertyname, $value)
14+
{
15+
#echo "This is Non-Existing and private property($property)";
16+
if (property_exists($this, $propertyname)) {
17+
$this->$propertyname = $value;
18+
} else {
19+
echo "Property Does Not Exist($propertyname)";
20+
}
21+
}
22+
}
23+
24+
$test = new abc();
25+
26+
echo $test->name = "Navjot Singh";
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
//This method is called when we attempt to access a private method from outside.
3+
4+
class student
5+
{
6+
private $first_name;
7+
private $last_name;
8+
9+
private function setName($fname, $lname)
10+
{
11+
$this->first_name = $fname;
12+
$this->last_name = $lname;
13+
}
14+
15+
public function show()
16+
{
17+
echo $this->first_name;
18+
echo $this->last_name;
19+
}
20+
//The method named $methodname will be called,
21+
//and the arguments that we passed to it will be available in the function as parameters.
22+
public function __call($methodname, $args)
23+
{
24+
#echo "This is Private Or Non-Existing method($methodname)";
25+
#print_r($args);
26+
if (method_exists($this, $methodname)) {
27+
call_user_func_array([$this, $methodname], $args);
28+
} else {
29+
echo "Method Does not Exist($methodname)";
30+
}
31+
}
32+
}
33+
34+
$test = new student();
35+
36+
$test->setName("Navjot", "Singh");
37+
print_r($test);
38+
39+
$test->show();

0 commit comments

Comments
 (0)