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?>
1class Bike {
2 function Bike() {
3 $this->type = 'BMX';
4 }
5}
6
7$blackSheep = new Bike();
8
9print $blackSheep->type;
1
2<?php
3class Foo {
4 public $aMemberVar = 'aMemberVar Member Variable';
5 public $aFuncName = 'aMemberFunc';
6
7
8 function aMemberFunc() {
9 print 'Inside `aMemberFunc()`';
10 }
11}
12
13$foo = new Foo;
14
15function getVarName()
16{
17 return 'aFuncName';
18}
19
20print $foo->{$foo->{getVarName()}}();
21
22
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
1The PHP Object-Oriented Programming concepts are:
2Class
3Objects
4Inheritance
5Interface
6Abstraction
7Magic Methods
1var numbers = [10, 20, 30, 40] // sums to 100var sum = 0;for (var i = 0; i < numbers.length; i++) { sum += numbers[i]}