php new class object code example

Example 1: php new object

By far the easiest and correct way to instantiate an empty generic php object that you can then modify for whatever purpose you choose:



<?php $genericObject = new stdClass(); ?>



I had the most difficult time finding this, hopefully it will help someone else!

Example 2: create a class in php

<?php
class Fruit {
  public $name;
  public $color;

  function __construct($name, $color) {
    $this->name = $name;
    $this->color = $color;
  }
  function get_name() {
    return $this->name;
  }
  function get_color() {
    return $this->color;
  }
}

$apple = new Fruit("Apple", "red");
echo $apple->get_name();
echo "<br>";
echo $apple->get_color();
?>

Example 3: php define class

class Bike {
    	function Bike() {
            $this->type = 'BMX';
    }
}

$blackSheep = new Bike();

print $blackSheep->type;

Tags:

Php Example