Jump to:

22983 Posts in 11709 Topics by 2827 members

General Questions

SilverStripe Forums » General Questions » how to include own php class ?

General questions about getting started with SilverStripe that don't fit in any of the categories above.

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Page: 1 2
Go to End
Author Topic: 4324 Views
  • snaip
    Avatar
    Community Member
    180 Posts

    how to include own php class ? Link to this post

    is there any way to include simple php class to SilverStripe ?
    i have CalendarCass in file calendarClass.php

    maby

    public static $has_one = array(
       "Calendar" => "CalendarClass"
    );

    ??

    and how to execute it than in the template ?
    in php file i wrote

    include_once 'calendarClass.php';
    $calendar = new Calendar();

  • Double-A-Ron
    Avatar
    Community Member
    599 Posts

    Re: how to include own php class ? Link to this post

    I did this recently with a custom class located within the ecommerce module using the module's DPS payment classes as an example.

    I simply got my custom class and made it extend Object. e.g.

    class DPS extends Object
    {
    var $PxPay_Key;
    var $PxPay_Url;
    var $PxPay_Userid;

    function __construct($Url, $UserId, $Key){
    error_reporting(E_ERROR);
    $this->PxPay_Key = $Key;
    $this->PxPay_Url = $Url;
    $this->PxPay_Userid = $UserId;
    }
    }

    The contents of the Class can remain as they would be if this were just a roll-your-own website.

    After that, no include statements are necessary as it is now hooked into SS and I can access the class from anywhere simply by calling

    $myDPS = new DPS($Url, $UserId, $Key);

    Aaron

  • Willr
    Avatar
    Forum Moderator
    5462 Posts

    Re: how to include own php class ? Link to this post

    As long as your code is in a parsable directory (such as mysite/code/*) then SS will include it (or you could include a require_once 'File.php' in the class that requires it).

    If you extend the class off object you do get access to a bunch of nice SS stuff like extensions and static stuff but note there is a performance hit when extending from object. We noticed a large performance gain when skipping this extension from classes which didn't require it.

  • Double-A-Ron
    Avatar
    Community Member
    599 Posts

    Re: how to include own php class ? Link to this post

    Thanks Will,

    Would a subdirectory of /mysite/code/ be classed as parsable? e.g. mysite/code/custom_helpers.

    I did the above in a module (ecommerce) and found that without extending a SS class like Object, I was not able to access my class without an include_once call.

    Aaron

  • Willr
    Avatar
    Forum Moderator
    5462 Posts

    Re: how to include own php class ? Link to this post

    Double-A-Ron - Yes any directory or sub directory of a folder with a _config file and no _manifest_exclude file will be included in the manifest. I am pretty SS the manifest builder should pick up any PHP classes regardless of what it extends as long as the ClassName == Filename you should be good.

  • dalesaurus
    Avatar
    Community Member
    283 Posts

    Re: how to include own php class ? Link to this post

    Yes it will traverse into nested subfolders, and don't forget that the Filename == Classname is gone in 2.4.

  • Willr
    Avatar
    Forum Moderator
    5462 Posts

    Re: how to include own php class ? Link to this post

    don't forget that the Filename == Classname is gone in 2.4.

    Thanks for the reminder. Forgot this feature.

  • snaip
    Avatar
    Community Member
    180 Posts

    Re: how to include own php class ? Link to this post

    i tried include simple php class i it works fine

    but i still have problem with executing my callendarClass

    hmm maby i will send You php files and You will help me to execute it ?

    4324 Views
Page: 1 2
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.