1<?php
2class Fruit {
3 public $name;
4 public $color;
5
6 function __construct($name, $color) {
7 $this->name = $name;
8 $this->color = $color;
9 }
10 function get_name() {
11 return $this->name;
12 }
13 function get_color() {
14 return $this->color;
15 }
16}
17
18$apple = new Fruit("Apple", "red");
19echo $apple->get_name();
20echo "<br>";
21echo $apple->get_color();
22?>
1
2<?php
3/**
4 * Define MyClass
5 */
6class MyClass
7{
8 public $public = 'Public';
9 protected $protected = 'Protected';
10 private $private = 'Private';
11
12 function printHello()
13 {
14 echo $this->public;
15 echo $this->protected;
16 echo $this->private;
17 }
18}
19
20$obj = new MyClass();
21echo $obj->public; // Works
22echo $obj->protected; // Fatal Error
23echo $obj->private; // Fatal Error
24$obj->printHello(); // Shows Public, Protected and Private
25
26
27/**
28 * Define MyClass2
29 */
30class MyClass2 extends MyClass
31{
32 // We can redeclare the public and protected properties, but not private
33 public $public = 'Public2';
34 protected $protected = 'Protected2';
35
36 function printHello()
37 {
38 echo $this->public;
39 echo $this->protected;
40 echo $this->private;
41 }
42}
43
44$obj2 = new MyClass2();
45echo $obj2->public; // Works
46echo $obj2->protected; // Fatal Error
47echo $obj2->private; // Undefined
48$obj2->printHello(); // Shows Public2, Protected2, Undefined
49
50?>
51
52
1
2/*PHP 5 is very very flexible in accessing member variables and member functions.
3These access methods maybe look unusual and unnecessary at first glance;
4but they are very useful sometimes;
5specially when you work with SimpleXML classes and objects.
6I have posted a similar comment in SimpleXML function reference section,
7but this one is more comprehensive.
8
9I use the following class as reference for all examples:
10*/
11
12<?php
13
14class Foo {
15
16 public $aMemberVar = 'aMemberVar Member Variable';
17 public $aFuncName = 'aMemberFunc';
18
19
20 function aMemberFunc() {
21 print 'Inside `aMemberFunc()`';
22 }
23}
24$foo = new Foo;
25?>
26/*You can access member variables in an object using another variable as name:*/
27
28<?php
29$element = 'aMemberVar';
30print $foo->$element; // prints "aMemberVar Member Variable"
31?>
32or use functions:
33
34<?php
35
36function getVarName()
37{ return 'aMemberVar'; }
38print $foo->{getVarName()}; // prints "aMemberVar Member Variable"
39
40?>
41
42
43
44//Important Note: You must surround function name with { and }
45 ///or PHP would think you are calling a member function of object "foo".
46//you can use a constant or literal as well:
47
48<?php
49
50define(MY_CONSTANT, 'aMemberVar');
51
52print $foo->{MY_CONSTANT}; // Prints "aMemberVar Member Variable"
53
54print $foo->{'aMemberVar'}; // Prints "aMemberVar Member Variable"
55
56?>
57//You can use members of other objects as well:
58
59<?php
60
61print $foo->{$otherObj->var};
62
63print $foo->{$otherObj->func()};
64
65?>
66
67//You can use mathods above to access member functions as well:
68
69<?php
70
71print $foo->{'aMemberFunc'}(); // Prints "Inside `aMemberFunc()`"
72
73print $foo->{$foo->aFuncName}(); // Prints "Inside `aMemberFunc()`"
74
75?>
76
77
1class Bike {
2 function Bike() {
3 $this->type = 'BMX';
4 }
5}
6
7$blackSheep = new Bike();
8
9print $blackSheep->type;
1var array = [1, 2, 3, 4, 5, 6],
2 s = 0,
3 p = 1,
4 i;
5for (i = 0; i < array.length; i += 1)
6 {
7 s += array[i];
8 p *= array[i];
9 }
10console.log('Sum : '+s + ' Product : ' +p);
11
12
1var numbers = [10, 20, 30, 40] // sums to 100var sum = 0;for (var i = 0; i < numbers.length; i++) { sum += numbers[i]}