-
Notifications
You must be signed in to change notification settings - Fork 0
/
class_magic_methots.php
49 lines (41 loc) · 1.79 KB
/
class_magic_methots.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
<?
class MyClass{ //Creating a class
//class properties and methods go here
public $prop1 = "I'm a class property!";//the word "public" defines the visibility of a class property
public function __construct(){//This magic method is trigged once a new object is created
//echo 'The class"', __CLASS__, '" was initiated!<br />';
echo "hola <br />";
}
public function __destruct(){//This magic method is trigged once the object
//echo 'This class was destroyed <br />';
echo "destroyed <br />";
}
public function __tostring(){//This method allows you to return the object as a string
return "";
}
public function setProperty($newval){
$this->prop1 = $newval; //Inside the class we call the object with "this" word
}
public function getProperty(){
return $this->prop1 . "<br />";
}
}
//$obj = new MyClass;
//echo 'destination" ', $obj, ' "I know';
//$obj->setProperty("This is the new name of my property");
//echo $obj->getProperty();
//echo $obj;
//unset($obj);//This method destroyes the object inmediatly
//echo "This is the end of the class <br />";
//--------------------------------INHERINANCE-----------------------------------
//Inheritance uses the word extends to indicate that the current class inherit from a class existent
//For instance the class myOtherlass inherit properties and functions from the class MyClass
class myOtherClass extends MyClass{
public function setProperty(){//Overwriting a method
return $this->prop1 . "<br />";
}
}
$myOtherObject = new myOtherClass;
//$myOtherObject->setProperty("This is the other object using methods and properties from MyClass class");
echo $myOtherObject->setProperty();
?>