Ejemplo n.º 1
0
 public function getAllBirds()
 {
     $db = new Db();
     $birds = array();
     $results = $db->select("SELECT\r\n        birds.bid AS 'bid',\r\n        birds.date AS 'date',\r\n        birds.species AS 'species',\r\n        birds.distance AS 'distance',\r\n        birds.migrant AS 'migrant',\r\n        birds.sex AS 'sex',\r\n        birds.nest AS 'nest',\r\n        birds.eggs as 'eggs',\r\n        users.user_name as 'userName'\r\n        FROM birds\r\n        INNER JOIN users ON\r\n        birds.users_user_id = users.user_id\r\n        ORDER BY bid DESC;");
     foreach ($results as $result) {
         $bird = new Bird();
         $bird->hydrate($result);
         $birds[] = $bird;
     }
     return $birds;
 }
Ejemplo n.º 2
0
 function random_bird($complete = false)
 {
     $bird_names = $this->all_bird_names();
     $random_bird_name = $bird_names[rand(0, count($bird_names) - 1)];
     $random_bird = new Bird($random_bird_name);
     if ($complete) {
         while ($random_bird->isComplete() === false) {
             $random_bird_name = $bird_names[rand(0, count($bird_names) - 1)];
             $random_bird = new Bird($random_bird_name);
         }
     }
     return $random_bird;
 }
Ejemplo n.º 3
0
 public function __construct($eyeColor, $song)
 {
     /*
      * explicitly call the parent constructor to ensure it is run
      * */
     parent::__construct($eyeColor, $song);
     echo "<p>A Penguin is born!</p>";
 }
Ejemplo n.º 4
0
class Bird extends Animal
{
    public function __construct($name, $height, $weight, $favFood, $speed, $sound, $canFly)
    {
        parent::__construct($name, $height, $weight, $favFood, $speed, $sound, $canFly);
    }
}
try {
    $dog = new Dog('dog', 3, 4, 'cat', 5, 'Bark', false);
    $playwithanimal = new PlayWithAnimal($dog);
    echo "<br>";
    $dog->tryToFly();
    echo "<br>";
    $dog->talk();
    echo "<br>";
    $cat = new Cat('cat', 3, 4, 'mice', 5, 'miow', false);
    $playwithanimal = new PlayWithAnimal($cat);
    echo "<br>";
    $cat->tryToFly();
    echo "<br>";
    $cat->talk();
    echo "<br>";
    $bird = new Bird('Bird', 3, 4, 'insect', 5, 'miow', true);
    $playwithanimal = new PlayWithAnimal($bird);
    echo "<br>";
    $bird->tryToFly();
    echo "<br>";
    $bird->talk();
} catch (Exception $e) {
    echo $e->getMessage();
}
Ejemplo n.º 5
0
        $birdManager->_update($bird);
        header('Location: user.php');
        break;
    case 'save_bird':
        $birdManager = new BirdManager();
        $arr = array();
        $arr["bid"] = isset($_GET["bid"]) ? $_GET["bid"] : '';
        $arr["users_user_id"] = isset($_GET["userID"]) ? $_GET["userID"] : '';
        $arr["date"] = isset($_GET["date"]) ? $_GET["date"] : '';
        $arr["species"] = isset($_GET["species"]) ? $_GET["species"] : '';
        $arr["eggs"] = isset($_GET["eggs"]) ? $_GET["eggs"] : '';
        $arr["nest"] = isset($_GET["nest"]) ? $_GET["nest"] : '';
        $arr["distance"] = isset($_GET["distance"]) ? $_GET["distance"] : '';
        $arr["migrant"] = isset($_GET["migrant"]) ? $_GET["migrant"] : '';
        $arr["sex"] = isset($_GET["sex"]) ? $_GET["sex"] : '';
        $arr["users_user_id"] = isset($_GET["users_user_id"]) ? $_GET["users_user_id"] : '';
        $bird = new Bird();
        $bird->hydrate($arr);
        $birdManager->save($bird);
        header('Location: user.php');
        break;
    default:
        $birdManager = new BirdManager();
        $birds = $birdManager->getMyBird($target);
        include '../views/user_bird_view_list.php';
        break;
}
?>
</body>
</html>
    }
    public function sing()
    {
        echo "<p>" . $this->song . "</p>";
    }
    public function demonstrateBirdness()
    {
        $this->fly();
        $this->sing();
    }
}
/*
 * instantiate the class
 * */
echo "<p>Create a new Bird Object...</p>";
$hummingbird = new Bird();
/*
try out the various functions
*/
echo "<p>Run sing() function...</p>";
$hummingbird->sing();
echo "<p>Run and echo getSong() function ...</p>";
echo $hummingbird->getSong();
echo "<p>Run setSong() to change the Brid's song...</p>";
$hummingbird->setSong("Caw caw!");
echo "<p>Run and echo the song to prove it has been changed...</p>";
echo $hummingbird->getSong();
echo "<p>Run demonstrateBirdness()...</p>";
$hummingbird->demonstrateBirdness();
echo "<p>Create another new Bird Object...</p>";
$eagle = new Bird();
Ejemplo n.º 7
0
	<h1>Class Design: Constructors</h1>
   <p>A constructor is a special function within a class definition, that is automatically invoked as soon as an object is instatiated. It provides a chance for the class designer to ensure properties are appropriately initialized.</p> 
   <p>PHP is unlike some other OOP languages, in that the constructor function does not share the name of the class, but is given the function name <strong><a href="http://php.net/manual/en/language.oop5.decon.php">__construct( )</a></strong> NOTE the double underscore preface.</p>
   <p>Officially,the constructor is one of many so-called 'magic functions' in PHP. Magic functions are reserved function names and have special behaviors, such as never being explicitly invoked by the developer (PHP invokes them automatically). All magic function names are prefaced with two underscores.</p> 
<pre><code>
class MyClass{
	public function __construct(){
		//this function is invoked automaticcalky
		//during Object instantiation
	}
}
</code></pre>
<span>
    <?php 
/*
load the Bird.php file
examine the source code and locate the constructor
determine if parameters will be required at instantiation
*/
require_once "Bird.php";
/*
pass parameter arguments to the constructor at instantiation
*/
$whiskeyJack = new Bird("Blue", "Tweet!");
$hawk = new Bird("Black", "Screech!");
$whiskeyJack->demonstrateBirdness();
?>
</span>

</body>
</html>
Ejemplo n.º 8
0
<? 
include ('bird.php');

$blue_jay=new Bird();

$blue_jay->sing(); //echos "Tweet!! Tweet!!"


?>
Ejemplo n.º 9
0
<?php

/**
 * Created by PhpStorm.
 * User: xpcomrade
 * Date: 2015/7/29
 * Time: 19:53
 */
trait Sortable
{
    abstract function uniqueId();
    function compareById($object)
    {
        return $object->uniqueId() < $this->uniqueId() ? -1 : 1;
    }
}
class Bird
{
    use Sortable;
    function uniqueId()
    {
        // TODO: Implement uniqueId() method.
        return __CLASS__ . ":{$this->id}";
    }
}
$bird = new Bird();
echo 'compare result:' . $bird->compareById($bird);
Ejemplo n.º 10
0
echo $obj->data;
$objectAsSerializedString = serialize($obj);

//...store $objectAsSerializedString in a DB or text file
//later, when its time to retrieve (re-instantiate) the object...

$newObj = unserialize( $objectAsSerializedString );
echo $newObj->data;
</code></pre>	
<span>
    <?php 
function __autoload($classname)
{
    require_once "./" . $classname . ".php";
}
$myBird = new Bird("Yellow", "Tweet");
$myBird->demonstrateBirdness();
echo "<h3>Serializing an Object into a string...</h3>";
$serializedString = serialize($myBird);
echo "<h3>Display the serialized string...</h3>";
echo "<p>" . $serializedString . "</p>";
/*
at this point, we could store the string in a cookie or database...

...later, when we need to retrieve the Object,  
use the string to re-instantiate the object with all the same property values
*/
echo "<h3>Unserializing an Object...</h3>";
$aReinstantiatedBird = unserialize($serializedString);
echo "<h3>Demonstrate the re-instantiated Object...</h3>";
$aReinstantiatedBird->demonstrateBirdness();
//store the object in a PHP variable
$myObject = new MyClass();
</code></pre>	
    <h4>OOP Basics: Using an Object</h4>
	<p>After an object is instantiated, you have access to any of its public behaviors or properties using the <code>-></code> operator.</p>
<pre><code>//instantiate an object from the class
$myObject = new MyClass();
//run the doSomething() function
// using the -> operator
$myObject->doSomething();
</code></pre>	
<span>
<?php 
//define a class
class Bird
{
    function fly()
    {
        echo "<p>Flap flap!</p>";
    }
}
//instantiate an object
echo "<h3>Instantiate a new Object</h3>";
$aBird = new Bird();
//run an object's public function
echo "<h3>Run a function</h3>";
$aBird->fly();
?>
</span>
</body>
</html>
Ejemplo n.º 12
0
    //accessor (usually returns the value)
    public function getSong()
    {
        return $this->song;
    }
    //mutator (usually takes a parameter(s) and )
    public function setSong($newSong)
    {
        if (!is_string($newSong)) {
            echo "<p>'" . $newSong . "' is not a reasonable song. Song has not been changed</p>";
        }
        $this->song = $newSong;
    }
}
echo "<h3>Instantiate a new Object</h3>";
$aBird = new Bird();
echo "<h3>Run public functions</h3>";
echo "<p>Run accessor getSsong(): " . $aBird->getSong() . "</p>";
echo "<p>Run mutator setSong() to change the song...</p>";
$aBird->setSong("Tweet tweet!");
echo "<p>Run accessor getSong(): " . $aBird->getSong() . "</p>";
echo "<h3>Try to setSong() to something unreasonable (eg: a number)...</h3>";
$aBird->setSong(42);
echo "<h3>If a property is public, it can potentially be abused...</h3>";
echo "<p>Public \$birdType is currently: " . $aBird->birdType . "</p>";
echo "<p>Re-assigning the value of \$birdType</p>";
$aBird->birdType = "Cheeseburger";
echo "<p>Public \$birdType is currently: " . $aBird->birdType . "</p>";
?>
</span>
</body>
loading the appropriate class file
*/
function __autoload($classFileName){
	require_once("path/to/classfiles/" . $classFileName  . ".php");
}

$obj1 = new MyClass01();	//__autoload() will load MyClass01.php
$obj2 = new MyClass02();  //__autoload() will load MyClass02.php
</code></pre>	
<span>
    <?php 
/*
autoload any/all classes needed by this page
if present, this function is automaticaly invoked by PHP
*/
function __autoload($classFileName)
{
    /*
    whenever code in this page needs a Class definition, 
    __autoload() will be invoked
    the $parameter will be the string name of the class needed
    */
    require_once $classFileName . ".php";
}
//instantiate a Bird, use it's functions
$budgie = new Bird("Black", "La la la");
$budgie->demonstrateBirdness();
?>
</span>
</body>
</html>
Ejemplo n.º 14
0
<?php

include "Dog.php";
include "Bird.php";
$animal = new Animal("jeffery the lion");
$animal->getName();
$doggy = new Dog("Timmy");
$doggy->getName();
$doggy->move();
$doggy->digHole();
$doggy->tryToFly();
$birdy = new Bird("Tweety");
$birdy->getName();
$birdy->move();
$birdy->tryToFly();
Ejemplo n.º 15
0
    $bird_names = $birdController->all_bird_names();
    echo "Search = " . $search_name;
    echo "<br />";
    $matching_birds = [];
    foreach ($bird_names as $name) {
        if (stripos($name, $search_name) !== false) {
            array_push($matching_birds, $name);
        }
    }
    echo count($matching_birds) . " Results";
    // echo "<br />";
    // if ($complete_birds_only) {
    //   echo "Results only include birds with complete information. Set onlycompletebirds to false if you want the results to contain all birds.";
    // } else {
    //   echo "onlycompletebirds = false";
    // }
    echo "<br />";
    echo "<br />";
    foreach ($matching_birds as $bird_name) {
        $bird = new Bird($bird_name);
        if ($complete_birds_only) {
            if ($bird->isComplete()) {
                $bird->print_info();
            }
        } else {
            $bird->print_info();
        }
    }
} else {
    echo "Search term is too short.";
}
Ejemplo n.º 16
0
    {
        echo $type . " Թռավ " . $this->wingLenght . " մետրանոց թեվերով<br>";
    }
}
class MarinAnimal extends Animal
{
    public $volume;
    public function swim()
    {
        echo $type . " Լողաց";
    }
}
/*
$dog = new Animal("Շուն",25);
echo "<pre>";
var_dump($dog);
echo "</pre>";

$dog->breath();
$dog->eath();
*/
$kalibri = new Bird("Կալիբլի", 25);
$kalibri->wingLenght = 0.25;
$kalibri->breath();
$kalibri->eath();
$kalibri->fly();
$dolpin = new MarinAnimal("Դելֆին", 10);
$dolpin->volume = 1000;
$dolpin->breath();
$dolpin->eath();
$dolpin->swim();
Ejemplo n.º 17
0
   <p>Similar to the constructor, a destructor <a href="http://php.net/manual/en/language.oop5.decon.php">__destruct()</a>is another 'magic' function within a class definition. It is automatically invoked as soon as an object is destroyed or when the script has terminated. It provides a chance for the class designer to execute code before an object has 'died'.</p> 	
<pre><code>
class MyClass{
	public function __destruct(){
		//this function is invoked automaticcalky
		//as soon as the Object is either
		// unset() or when the script terminates
	}
}
</code></pre>
<span>
    <?php 
require_once "Bird.php";
echo "<h3>Instantiate a couple of Birds...</h3>";
//instantiate a couple of Birds
$whiskeyJack = new Bird("Blue", "Caw caw!");
$eagle = new Bird("Black", "Screech!");
//try out one of the functins
$whiskeyJack->demonstrateBirdness();
/*  
note how when you unset() an Object, the destructor will be invoked
*/
echo "<h3>Now we will explicitly unset (destroy) the first Bird</h3>";
unset($whiskeyJack);
//destroy the whiskeyJack Object
/*
note how if we do not unset the Objects, they will be unset for us once the script has finished running
*/
//no unsetting required for the eagle Object, it will be unset for us
?>
</span>