Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

General Questions /

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

how to include own php class ?


Go to End
Reply


10 Posts   4755 Views

Avatar
snaip

Community Member, 180 Posts

4 December 2009 at 4:13am

Edited: 04/12/2009 4:14am

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();

Avatar
Double-A-Ron

Community Member, 604 Posts

4 December 2009 at 8:17am

Edited: 04/12/2009 8:17am

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

Avatar
Willr

Forum Moderator, 5513 Posts

4 December 2009 at 9:17am

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.

Avatar
Double-A-Ron

Community Member, 604 Posts

4 December 2009 at 10:25am

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

Avatar
Willr

Forum Moderator, 5513 Posts

4 December 2009 at 10:40am

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.

Avatar
dalesaurus

Community Member, 283 Posts

4 December 2009 at 11:55am

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

Avatar
Willr

Forum Moderator, 5513 Posts

4 December 2009 at 12:08pm

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

Thanks for the reminder. Forgot this feature.

Avatar
snaip

Community Member, 180 Posts

5 December 2009 at 2:04am

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 ?

Go to Top