Methods

Methods

Methods are functions defined within a class declaration. They are of two types, those which can be accessed without first declaring an instance of the class and those which can only be accessed by an instance of a class.

Ruby calls the first type a “class method” and the second type an “instance method”. It implements them this way:

class Example
  def Example.class_method
  end
  def instance_method
  end
end

and calls them this way:

  result = Example.class_method
  example = Example.new
  result = example.instance_method

PHP calls the first type a “public static” method and the second type is just a function.

<?php
  class Example {
    public static function class_method() {
//    You can't use $this in a class method.
//    You can't use -> to refer to a class method.  You must use ::
    }
    function instance_method() {
    }
  }
?>

and calls them this way:

<?php
  $result = Example::class_method();
  $example = new Example;
  $result = $example->instance_method();
?>

More information about class methods (and class variables) is in the PHP manual under Scope Resolution Operator (::).

 
methods.txt · Last modified: 2011/02/09 14:56 by 87.223.228.166
 

The Akelos Framework was created by Bermi Ferrer and other contributors.
Potions of the code and documentation have been ported from Ruby on Rails.

The Akelos Framework is released under the LGPL license.

"Akelos", "Akelos Framework", and the Akelos logo are trademarks of Bermi Labs All rights reserved.

Wiki driven by DokuWiki