21286 Posts in 5733 Topics by 2602 members
| Go to End | Next > | |
| Author | Topic: | 3513 Views |
-
how to include own php class ?

4 December 2009 at 4:13am Last edited: 4 December 2009 4:14am
is there any way to include simple php class to SilverStripe ?
i have CalendarCass in file calendarClass.phpmaby
public static $has_one = array(
"Calendar" => "CalendarClass"
);??
and how to execute it than in the template ?
in php file i wroteinclude_once 'calendarClass.php';
$calendar = new Calendar(); -
Re: how to include own php class ?

4 December 2009 at 8:17am Last edited: 4 December 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
-
Re: how to include own php class ?

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.
-
Re: how to include own php class ?

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
-
Re: how to include own php class ?

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.
-
Re: how to include own php class ?

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.
-
Re: how to include own php class ?

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.
-
Re: how to include own php class ?

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 ?
| 3513 Views | ||
| Go to Top | Next > |




