Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
php-object-oriented/index.php
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
114 lines (102 sloc)
2.29 KB
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
<html> | |
<head> | |
<title>Class and Object Methods</title> | |
</head> | |
<body> | |
<p> | |
<?php | |
class Person { | |
public $isAlive = true; | |
function __construct($name) { | |
$this->name = $name; | |
} | |
public function dance() { | |
return "I'm dancing!"; | |
} | |
} | |
$me = new Person("Shane"); | |
if (is_a($me, "Person")) { | |
echo "I'm a person, "; | |
} | |
if (property_exists($me, "name")) { | |
echo "I have a name, "; | |
} | |
if (method_exists($me, "dance")) { | |
echo "and I know how to dance!"; | |
} | |
?> | |
</p> | |
<p> | |
<?php | |
class Shape { | |
public $hasSides = true; | |
} | |
class Square extends Shape { | |
} | |
$square = new Square(); | |
// Add your code below! | |
if (property_exists($square, "hasSides" )) { | |
echo "I have sides!"; | |
} | |
?> | |
</p> | |
<p> | |
<?php | |
class Vehicle { | |
final public function honk() { | |
return "I'm driving here!"; | |
} | |
} | |
// Add your code below! | |
class Bicycle extends Vehicle { | |
public function honk() { | |
return "Beep Beep!"; | |
} | |
} | |
$bicycle = new Bicycle(); | |
$bicycle->honk(); | |
echo "Beep Beep!"; | |
?> | |
</p> | |
<p> | |
<?php | |
class Person { | |
} | |
class Ninja extends Person { | |
// Add your code here... | |
const stealth = 'MAXIMUM'; | |
} | |
// ...and here! | |
if (Ninja::stealth) { | |
echo "MAXIMUM"; | |
} | |
?> | |
</p> | |
<p> | |
<?php | |
class King { | |
// Modify the code on line 10... | |
public static function proclaim() { | |
echo "A kingly proclamation!"; | |
} | |
} | |
// ...and call the method below! | |
King::proclaim(); | |
?> | |
</p> | |
<p> | |
<?php | |
class Person { | |
public static function say() { | |
echo "Here are my thoughts!"; | |
} | |
} | |
class Blogger extends Person { | |
const cats = 50; | |
} | |
Blogger::say(); | |
echo Blogger::cats; | |
?> | |
</p> | |
</body> | |
</html> |