You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: doc/classes.md
+17-16Lines changed: 17 additions & 16 deletions
Original file line number
Diff line number
Diff line change
@@ -17,8 +17,8 @@
17
17
```python
18
18
classmyClass:
19
19
foo =1
20
-
defreturnFoo(self):
21
-
returnself.foo
20
+
defreturnFoo(self):
21
+
returnself.foo
22
22
23
23
```
24
24
@@ -37,9 +37,10 @@ foo = myClass()
37
37
PHP classes are constructed by defining a [`__construct()`](https://secure.php.net/manual/en/language.oop5.decon.php) method.
38
38
```php
39
39
<?php
40
-
class myClass {
41
-
function __construct($foo) {
42
-
$this->foo = $foo;
40
+
class myClass {
41
+
public $foo;
42
+
function __construct($foo) {
43
+
$this->foo = $foo;
43
44
}
44
45
}
45
46
?>
@@ -48,18 +49,18 @@ PHP classes are constructed by defining a [`__construct()`](https://secure.php.n
48
49
Python classes are constructing by defining a [`__init__`](https://docs.python.org/2/reference/datamodel.html#object.__init__) method.
49
50
```python
50
51
classmyClass:
51
-
def__init__(self, foo):
52
-
self.foo = foo
52
+
def__init__(self, foo):
53
+
self.foo = foo
53
54
```
54
55
55
56
### Class deconstruction/deinitialization
56
57
#### PHP
57
58
Destructor in PHP will be called once there exist no references to an object. These are created by defining a [`__destruct()`](https://secure.php.net/manual/en/language.oop5.decon.php#destructor) method.
58
59
```php
59
60
<?php
60
-
class myClass {
61
-
function __destruct() {
62
-
echo "Goodbye";
61
+
class myClass {
62
+
function __destruct() {
63
+
echo "Goodbye";
63
64
}
64
65
}
65
66
?>
@@ -70,20 +71,20 @@ Python supports deconstruction in two ways. One, when an object is created with
70
71
The `__del__()` method will be called when Python's garbage collector deallocates its memory once there are no longer any references to the object. It should be noted that use of this function is generally frowned upon by most Python developers, who instead either construct their objects in such a way to be used as context managers, or let the native Python garbage collector handle object deconstruction.
71
72
```python
72
73
classmyClass:
73
-
def__init__(self, foo):
74
-
self.foo = foo
74
+
def__init__(self, foo):
75
+
self.foo = foo
75
76
def__del__(self):
76
-
print("Goodbye")
77
+
print("Goodbye")
77
78
```
78
79
79
80
The other form of object deinitialization is when an object is being used as a context manager. This behavior is not called specifically when the object is about to be deallocated, but rather at the end of some logical operation. In this way, developers can manually and specifically call cleanup methods, reset variables, etc. while within a particular context. Practically, this means objects will only have their exit called when being treated in code as a context manager, which means being called with the `with` keyword. The "deconstructor" itself is created by defining a `__exit__()` method:
80
81
81
82
```python
82
83
classmyClass:
83
-
def__init__(self):
84
-
print("Object has been created")
84
+
def__init__(self):
85
+
print("Object has been created")
85
86
def__enter__(self):
86
-
print("Object entered with runtime context")
87
+
print("Object entered with runtime context")
87
88
def__exit__(self):
88
89
print("Object called as runtime context is ending")
0 commit comments