Update page 'P# 1.0 Draft Specification'
@@ -127,6 +127,8 @@ P# code can be split in multiple files for bigger scripts. There are four statem
|
||||
* require
|
||||
* require_one
|
||||
|
||||
All of above listed builtin functions can be used from any place, even outside of the class definition.
|
||||
|
||||
## 5. Data Types & Pseudo Types
|
||||
### 5.1. Boolean Values
|
||||
The bool type represents Boolean logical quantities. The possible values of type bool are TRUE and FALSE. In P#, a zero integral or floating-point value, or a null pointer can be converted to the Boolean value FALSE, and a non-zero integral or floating-point value, or a non-null pointer can be converted to the Boolean value TRUE.
|
||||
@@ -359,25 +361,150 @@ The above expression in PHP will output 't' and this is an unexpected result, bu
|
||||
### 7.7. Break & Continue Statements
|
||||
|
||||
## 8. Object-Oriented Programming
|
||||
There are three widely used programming paradigms there: procedural programming, functional programming, and object-oriented programming. P# supports only object-oriented programming (OOP). This is a programming paradigm that uses objects and their interactions to design applications and computer programs. The basic programming concepts in OOP are:
|
||||
* Abstraction
|
||||
* Polymorphism
|
||||
* Encapsulation
|
||||
* Inheritance
|
||||
|
||||
### 8.1. Objects in P#
|
||||
Objects are basic building blocks of a P# script. An object is a combination of data and methods. In a OOP program, we create objects. These objects communicate together through methods. Each object can receive messages, send messages, and process data. There are two steps in creating an object. First, a class needs to be defined. A class is a template for an object. It is a blueprint which describes the state and behavior that the objects of the class all share. A class can be used to create many objects. Objects created at runtime from a class are called instances of that particular class.
|
||||
|
||||
<%
|
||||
class Demo { }
|
||||
object $demo_object = new Demo();
|
||||
%>
|
||||
|
||||
Because P# supports only OOP, the sample working script that prints 'Hello World!' message can look like example below:
|
||||
|
||||
<%
|
||||
class Main {
|
||||
|
||||
// Constructor, script entry point
|
||||
void __construct() {
|
||||
string $msg = 'Hello World!';
|
||||
print($msg);
|
||||
}
|
||||
|
||||
} /* class */
|
||||
%>
|
||||
|
||||
In above example, Main class is defined which is being automatically instantiated by the P# Interpreter. During instantiation, class constructor is automatically called.
|
||||
|
||||
### 8.2. Object Attributes
|
||||
Object attributes are the data bundled in an instance of a class. The object attributes are called instance variables or member fields. An instance variable is a variable defined in a class, for which each object in the class has a separate copy.
|
||||
|
||||
<%
|
||||
class Person {
|
||||
public string $name = '';
|
||||
}
|
||||
|
||||
class Main {
|
||||
|
||||
void Main() {
|
||||
object $person1, $person2;
|
||||
|
||||
$person1 = new Person();
|
||||
$person2 = new Person();
|
||||
$person1->name = "John";
|
||||
$person2->name = "Olivier";
|
||||
|
||||
print("Hello $person1->name and $person2->name");
|
||||
}
|
||||
}
|
||||
%>
|
||||
|
||||
In example above Person class contains one attribute - $name. This class has been instantiated twice and both objects contains different data stored.
|
||||
|
||||
### 8.3. Methods
|
||||
Methods are functions defined inside the body of a class. They are used to perform operations with the attributes of objects. Methods are essential in encapsulation concept of the OOP paradigm.
|
||||
|
||||
### 8.4. Method Overloading
|
||||
<%
|
||||
class Circle {
|
||||
private float $radius;
|
||||
|
||||
### 8.5. Constructors & Destructors
|
||||
public void setRadius($radius) {
|
||||
$this->radius = $radius;
|
||||
}
|
||||
|
||||
### 8.6. Constructor Overloading
|
||||
public float area() {
|
||||
return $this->radius * $this->radius * 3.14;
|
||||
}
|
||||
}
|
||||
|
||||
### 8.7. Inheritance
|
||||
class Main {
|
||||
|
||||
### 8.8. Virtual Classes & Methods
|
||||
public void __construct() {
|
||||
object $c = new Circle();
|
||||
$c->setRadius(5);
|
||||
print('Area is: ' . $c->area());
|
||||
}
|
||||
|
||||
### 8.9. Magic Methods
|
||||
}
|
||||
%>
|
||||
|
||||
### 8.10. Access Modifiers
|
||||
Example above shows a Circle class. It contains one member field. It is the radius of the circle. The private keyword is an access specifier. It tells that the variable is accessible only from inside of the class. There are also 2 methods definition: area() and setRadius(). The $this keyword is a special variable which is used to access the member fields from methods. The area() method returns the area of a circle. Second class Main and its constructor are the entry point. Execution of the script begins from this point.
|
||||
|
||||
### 8.4. Access Modifiers
|
||||
Access modifiers set the visibility of methods and member fields. P# implements three access modifiers: public, protected, and private. The public members can be accessed from anywhere. The protected members can be accessed only within the class itself and by inherited and parent classes. The private members may only be accessed by the class that defines the member. Access modifiers protect data against accidental modifications. They make the programs more robust.
|
||||
|
||||
<%
|
||||
class DemoClass {
|
||||
public float $real;
|
||||
protected string $text;
|
||||
private int $age;
|
||||
}
|
||||
|
||||
class Demo extends DemoClass {}
|
||||
|
||||
%>
|
||||
|
||||
In the above script, there are three member fields; one is declared public, second protected and the last as private. Public visible attribute can be modified from anywhere. Protected argument, from DemoClass and Demo classes. Private argument can be modified only from DemoClass.
|
||||
If there is no access modifier specified, public is assumed.
|
||||
|
||||
### 8.5. Method Overloading
|
||||
Method overloading allows the creation of several methods with the same name which differ from each other in the type of the input. P# implements a method overloading, well-known from languages like C#, Java or C++.
|
||||
|
||||
<%
|
||||
virtual class Test {
|
||||
virtual void function test();
|
||||
virtual void function test(int $var);
|
||||
}
|
||||
%>
|
||||
|
||||
### 8.6. Constructors & Destructors
|
||||
A constructor and destructor are a special kind of methods. Copnstructor is automatically called when the object is created. The purpose of the constructor is to initiate the state of the object. Destructor is automatically called when object is destroyed. The purpose of destructor is to close open handles (eg. close database connection).
|
||||
|
||||
<%
|
||||
class Demo {
|
||||
public void __construct() {
|
||||
}
|
||||
|
||||
public void __destruct() {
|
||||
}
|
||||
}
|
||||
%>
|
||||
|
||||
Both constructor and destructor methods cannot return any value. Declaring a constructor with access modifier other to public will make the class uninstantiable, as it will be unable call private or protected method from outside this class. Solution to this can be some additional method that will return instantiate the object and return it:
|
||||
|
||||
<%
|
||||
class Demo {
|
||||
private void __construct() {
|
||||
}
|
||||
|
||||
public object instance() {
|
||||
return new Demo();
|
||||
}
|
||||
}
|
||||
%>
|
||||
|
||||
### 8.7. Constructor Overloading
|
||||
|
||||
### 8.8. Inheritance
|
||||
|
||||
### 8.9. Virtual Classes & Methods
|
||||
|
||||
### 8.10. Magic Methods
|
||||
|
||||
### 8.11. Class Constants
|
||||
|
||||
|
||||
Reference in New Issue
Block a user