Skip to content

Extension BaseObject class

Dmitriy Zayceff edited this page Feb 21, 2016 · 4 revisions

Default class structure:

package org.jphp.mysuperext;

import static php.runtime.annotation.Reflection.*;
import php.runtime.env.Environment;
import php.runtime.lang.BaseObject;
import php.runtime.reflection.ClassEntity;

@Name("Some\\NamespaceName\\ClassName")
public class Olala extends BaseObject {
    public Olala(Environment env) {
        super(env);
    }

    public Olala(Environment env, ClassEntity clazz) {
        super(env, clazz);
    }
}

This class will be converted to:

<?php
namespace Some\NamespaceName;

class ClassName 
{
}

You can add more class-annotations for customize your result:

...
import php.runtime.reflection.ClassEntity;

@Abstract // if you want abstract class
@Final    // if you want final class
@Name("Some\\NamespaceName\\ClassName")
public class Olala extends BaseObject {
    public Olala(Environment env) {
...

You can not write @Abstract or @Final. Add final keyword inside your class for creating final class or abstract keyword for creating abstract class. Without target annotations.

Add methods

    ...

    @Signature
    public Memory __construct(Environment env, Memory... args) {
        // do something
        return Memory.NULL;
    }

    ...

Note: Each method should return Memory object. See: https://github.com/jphp-compiler/jphp/wiki/For-Contributors for more info about Memory objects.

This is __construct method. It will look like this:

<?php
namespace Some\NamespaceName;

class ClassName 
{
    public function __construct()
    {
    }
}

Then we can customize own method:

    ...

    @Signature({
            @Arg(value = "fieldOne"),
            @Arg(value = "fieldTwo"),
    })
    public Memory __construct(Environment env, Memory... args) {
        // do something
        return Memory.NULL;
    }

    ...
<?php
namespace Some\NamespaceName;

class ClassName 
{
    public function __construct($fieldOne, $fieldTwo)
    {
    }
}

@Arg anotation have target options:

  • value: arg name
  • typeClass: Php class name. @Arg(value = "file", typeClass = "php\\io\\File")
  • type: Argument type. @Arg(value = "callback", type = HintType.CALLABLE)
    • HintType.ANY
    • HintType.STRING
    • HintType.INT
    • HintType.DOUBLE
    • HintType.NUMBER
    • HintType.BOOLEAN
    • HintType.SCALAR
    • HintType.ARRAY
    • HintType.OBJECT
    • HintType.CALLABLE
    • HintType.VARARG
    • HintType.TRAVERSABLE
  • optional: Make argument optional. @Arg(value = "recursive", optional = @Optional("true"))
    • @Optional("true") -> (bool)true
    • @Optional("false") -> (bool)false
    • @Optional("0") -> (int)0
    • @Optional("100500") -> (int)100500
    • @Optional("null") -> null
    • @Optional("some") -> (string)"some"
    • @Optional("asdasd") -> (string)"asdasd"

Note: For create a static method add static keyword inside java method declaration

Properties (Fields)

Java code:

    ...
    @Property
    public String value = 23;
    ...

Will be convert to:

   ...
   public $value = 23;
   ...

You can create getters and setters:

    private String field = "fieldValue";

    @Getter("fieldName")
    public Memory getFieldName(Environment env, Memory... args) {
        return StringMemory.valueOf(this.field);
    }

    @Setter("fieldName")
    public Memory setFieldName(Environment env, Memory... args) {
        this.field = StringMemory.valueOf(args[0].toString() + "abc");
        return Memory.NULL;
    }

Will be convert to:

   ...
   public $fieldName; 
   // will return "fieldValue" when taking value of this field 
   // and add "abc" at end when sets a new value
   ...

Constants

   ...
   final public static String MY_CONSTANT_1 = "deadbeef";
   final public static int MY_CONSTANT_2    = 42;
   ...
<?php
namespace Some\NamespaceName;

class ClassName 
{
    const MY_CONSTANT_1 = "deadbeef";
    const MY_CONSTANT_2 = 42;
}
Clone this wiki locally