Last active
December 27, 2015 12:59
-
-
Save robynitp/7329393 to your computer and use it in GitHub Desktop.
Basic OOP in PHP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// --- PHP --- // | |
// Declare and construct an object from the class HLine | |
// PHP doesn't care whether the parameters are ints or strings | |
$h1 = new HLine(20, 'meow'); | |
// show the ypos property of h1 | |
echo $h1->ypos; | |
class HLine{ | |
/* | |
Properties and variables don't need data types in PHP | |
Generally they are declared public, private, or protected. Public is the default | |
*/ | |
public $ypos, $speed | |
/* | |
In PHP, the constructor is always named the same: __construct() | |
'public' is optional. | |
The method parameters don't need data types | |
-- they can be integers, strings, whatever | |
Like other variables in PHP, they need the dolloar sign ($) | |
*/ | |
public function __construct($y, $s){ | |
/* | |
ypos and speed are properties of the class | |
y and s are parameters; they are local to the function | |
properties have to use $this | |
*/ | |
$this->ypos = $y; | |
$this->speed = $s; | |
} | |
// no data types needed | |
public function doSomething($num) { | |
// do something | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment