Skip to content
This repository has been archived by the owner on Jan 16, 2019. It is now read-only.

Docs BasicMethods

Frank Kleine edited this page Apr 7, 2012 · 1 revision

Table of Contents

Basic methods available in (nearly) all classes of Stubbles

Nearly every class in Stubbles implements the net::stubbles::lang::stubObject interface. This allows to have five common methods for simple operations on objects, e.g. comparing via equals($compare) and basic object-to-string conversion via __toString().

In Stubbles two base classes implementing this interface exist: net::stubbles::lang::stubBaseObject and net::stubbles::lang::exceptions::stubException. While the latter one is the base class of all exceptions in Stubbles the former one is used as base class for all other classes except noted otherwise.

There are two exceptions from the rule. First, all classes marked as static do not have these methods as they would not make any sense in such classes. Second, classes in Stubbles extending built-in PHP classes will not implement this interface as well to prevent naming conflicts with existing methods in those built-in PHP classes. However it is possible that some of the methods are implemented without implementing the whole interface. We strive to always have a __toString() and an equals($compare) method in every class which allows to create an instance of it.

getClass()

This method will return an instance of <pre>net::stubbles::reflection::stubReflectionObject (see reflection docs for more information about the extended reflection classes). This allows a simpler approach to inspect the object.

getPackage()

This method returns an instance of <pre>net::stubbles::reflection::stubReflectionPackage (see reflection docs which describes the package where the class is located in. Unfortunately PHP < 5.3 doesn't have a concept of namespaces, therefore it is more like syntactic sugar. We at Stubbles believe that the language will strongly benefit from introducing namespaces and are excited for the upcoming PHP 5.3 release. The lack of namespaces lead us to the requirement to prefix all Stubbles classes with stub which will be gone once PHP 5.3 ships and we migrate to real namespaces.

getClassName()

This method returns the full qualified class name as a string. To obtain the non qualified class name you may use the class registry of stubClassLoader.

getPackageName()

This method returns the name of the package where the class is located in.

hashCode()

This method returns a unique identifier for any instance of an object. The default implementations available in <pre>net::stubbles::lang::stubBaseObject and <pre>net::stubbles::lang::exceptions::stubException rely on the spl_object_hash() function.

equals($compare)

This method allows to compare any value to the class instance. The default implementations available in <pre>net::stubbles::lang::stubBaseObject and <pre>net::stubbles::lang::exceptions::stubException return true if $compare is an instance of the same class and both instance have the same hash code. If a class behaves otherwise this will explicitly be documented.

__toString()

This method allows an automatic conversion of the object instance into a string. See PHP manual for more details.

The result is a short but informative representation about the class and its values. Per default, this method returns:

 [fully-qualified-class-name] ' {' [members-and-value-list] '}'

An example:

example::MyClass {
    foo(string): hello
    bar(example::AnotherClass): example::AnotherClass {
        baz(int): 5
    }
}

This notation says that the instance of the example::MyClass class has a property foo of type string with the value hello, and a property named bar of type example::AnotherClass which itself has a property named baz of type integer with value 5.

The result for exceptions in Stubbles is a bit differant:

example::MyException {
    message(string): This is an exception.
    file(string): /path/to/file.php
    line(integer): 4
    code(integer): 3
}

newInstance()

Available since 2.0.0

If your class extends from net::stubbles::lang::stubBaseObject it features a static constructor method which can be used to allow method chaining on construction of objects.

$myClass = MyExampleClass::newInstance('someConstructorArgument')->doSomething();

The newInstance() will create a new instance of the class the method was called on, passing all given arguments to the default constructor of the class.

Please note that this is not part of the default net::stubbles::lang::stubObject definition.

Clone this wiki locally