Created
November 16, 2017 17:48
-
-
Save Sstobo/603b4e7fd6915ac30a7ccf4326796f52 to your computer and use it in GitHub Desktop.
[Classes in PHP] #php #class
This file contains hidden or 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
class Dog { | |
public $name; | |
public $breed; #######properties | |
public $colour; | |
public function bark() { | |
return 'Woof!'; | |
} | |
public function walk() { | |
return 'Happy dog :)'; | |
} | |
} | |
$dog_1 = new Dog(); | |
$dog_1->name = 'Gomez'; #### change property | |
$dog_1->breed = 'Mixed'; | |
$dog_1->colour = 'white'; | |
echo $dog_1->bark(); ##### call method | |
$first_post = new Blog_Post(); | |
$second_post = new Blog_Post(); | |
$first_post->publish(); | |
class Blog_Post { ########### constructor works first | |
public $author; | |
public $publish_date; | |
public $is_published = false; | |
function __construct() { | |
$this->author = ''; | |
$this->publish_date = null; | |
} | |
// Other class methods here... | |
} | |
############### scoping | |
information that should only be accessible and relevant to it should remain private | |
information that should be accessible by itself and its subclasses should be protected | |
information that should be accessible by third-party objects and subclasses should be public |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment