Object Oriented Programming

Class Definition

<?php

  class Cart {

    var $items;

    function add_item($artnr, $num) {
        $this->items[$artnr] += $num;
    }  

  } 

?>

Inheriting a class with a Constructor

<?php

  class NamedCart extends Cart {

    var $owner;

    function __construct($name) {

        $this->owner = $name;

    }

  } 

?>

Invocation

<?php

  $cart = new NamedCart("PenguinGear");
  $cart->add_item(170923, 2);

?>

Static Method calls

<?php
  class foo {
    function bar() {
        echo "bar() called";
    }
  }

  foo::bar();
?>

Output

bar() called

Calling methods in your parent class

 

<?php

  class foo2 {

    function __construct() {
        echo "Constructor for foo2";
    }

  }

  class bar extends foo2 {

    function __construct() {

        echo "Constructor for bar<br />";

        $name = get_parent_class($this);
        parent::$name();

    }

  }

  $a = new bar();

?>

Output

Constructor for bar
Constructor for foo2

 


Loading ...

About the Author