Skip to content

Commit 56178ba

Browse files
authored
Update classes.md
1 parent e16130d commit 56178ba

File tree

1 file changed

+17
-16
lines changed

1 file changed

+17
-16
lines changed

doc/classes.md

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717
```python
1818
class myClass:
1919
foo = 1
20-
def returnFoo(self):
21-
return self.foo
20+
def returnFoo(self):
21+
return self.foo
2222

2323
```
2424

@@ -37,9 +37,10 @@ foo = myClass()
3737
PHP classes are constructed by defining a [`__construct()`](https://secure.php.net/manual/en/language.oop5.decon.php) method.
3838
```php
3939
<?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;
4344
}
4445
}
4546
?>
@@ -48,18 +49,18 @@ PHP classes are constructed by defining a [`__construct()`](https://secure.php.n
4849
Python classes are constructing by defining a [`__init__`](https://docs.python.org/2/reference/datamodel.html#object.__init__) method.
4950
```python
5051
class myClass:
51-
def __init__(self, foo):
52-
self.foo = foo
52+
def __init__(self, foo):
53+
self.foo = foo
5354
```
5455

5556
### Class deconstruction/deinitialization
5657
#### PHP
5758
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.
5859
```php
5960
<?php
60-
class myClass {
61-
function __destruct() {
62-
echo "Goodbye";
61+
class myClass {
62+
function __destruct() {
63+
echo "Goodbye";
6364
}
6465
}
6566
?>
@@ -70,20 +71,20 @@ Python supports deconstruction in two ways. One, when an object is created with
7071
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.
7172
```python
7273
class myClass:
73-
def __init__(self, foo):
74-
self.foo = foo
74+
def __init__(self, foo):
75+
self.foo = foo
7576
def __del__(self):
76-
print("Goodbye")
77+
print("Goodbye")
7778
```
7879

7980
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:
8081

8182
```python
8283
class myClass:
83-
def __init__(self):
84-
print("Object has been created")
84+
def __init__(self):
85+
print("Object has been created")
8586
def __enter__(self):
86-
print("Object entered with runtime context")
87+
print("Object entered with runtime context")
8788
def __exit__(self):
8889
print("Object called as runtime context is ending")
8990
def doStuff(self):

0 commit comments

Comments
 (0)