PHP » PYTHON |
login |
register |
about
|
PYTHON The Basics
is this article helpful?
|
Python replacement for PHP's The Basics
[
edit
| history
]
A simple class definition:
class SimpleClass: # instance variables are initialized in the constructor def __init__(self): self.var = 'a default value' # method declaration def displayVar(self): print self.var Note: in Python 2.x, the above actually defines an "old-style class". To define a new-style class, you must explicitly inherit (see below) from "object" or some other new-style class. In Python 3.x, all classes are new-style classes, so you don't have to worry about this. There is no special "this" variable in Python. Instead, when a method is called on an instance, the instance is passed as the first argument to the method. By convention, this argument is called "self", but it doesn't have to be. # Assuming that myObj is an instance of MyClass, then this method call: myObj.foo(bar, baz) # is equivalent to this one: MyClass.foo(myObj, bar, baz) To create a new instance, simply call the class as if it were a function: instance = SimpleClass() Assigning variables: assigned = instance Inheritance: class ExtendClass(SimpleClass): # Redefine the parent method def displayVar(self): print "Extending class" super(ExtendClass, self).displayVar() # in Python 3.x, you can do this instead: # super().displayVar() extended = ExtendClass() extended.displayVar() The BasicsclassEvery class definition begins with the keyword class, followed by a class name, which can be any name that isn't a reserved word in PHP. Followed by a pair of curly braces, which contains the definition of the classes members and methods. A pseudo-variable, $this is available when a method is called from within an object context. $this is a reference to the calling object (usually the object to which the method belongs, but can be another object, if the method is called statically from the context of a secondary object). This is illustrated in the following examples: Example #1 $this variable in object-oriented language
<?phpThe above example will output: $this is defined (a) $this is not defined. $this is defined (b) $this is not defined.
Example #2 Simple Class definition
<?phpThe default value must be a constant expression, not (for example) a variable, a class member or a function call. Example #3 Class members' default value
<?php
Unlike heredocs, nowdocs can be used in any static data context. Example #4 Static data example
<?php
newTo create an instance of a class, a new object must be created and assigned to a variable. An object will always be assigned when creating a new object unless the object has a constructor defined that throws an exception on error. Classes should be defined before instantiation (and in some cases this is a requirement). Example #5 Creating an instance
<?phpIn the class context, it is possible to create a new object by new self and new parent. When assigning an already created instance of a class to a new variable, the new variable will access the same instance as the object that was assigned. This behaviour is the same when passing instances to a function. A copy of an already created object can be made by cloning it. Example #6 Object Assignment
<?phpThe above example will output:
NULL
NULL
object(SimpleClass)#1 (1) {
["var"]=>
string(30) "$assigned will have this value"
}
extendsA class can inherit methods and members of another class by using the extends keyword in the declaration. It is not possible to extend multiple classes, a class can only inherit one base class. The inherited methods and members can be overridden, unless the parent class has defined a method as final, by redeclaring them with the same name defined in the parent class. It is possible to access the overridden methods or static members by referencing them with parent:: Example #7 Simple Class Inheritance
<?phpThe above example will output: Extending class a default value |
more
Recently updated
more
Most requested
|