Muhammad Gelbana
Muhammad Gelbana

Reputation: 4010

Unable to use php project (cron-expressions)

I'm trying to use this project. I downloaded the files. Got the project's folder, uncompressed it and then copied the directory "Cron" under the "src" directory to another location where I created the listed php file to test the project. The "Cron" folder contained the following files:

AbstractField.php
CronExpression.php //Main class
DayOfMonthField.php
DayOfWeekField.php
FieldFactory.php
FieldInterface.php
HoursField.php
MinutesField.php
MonthField.php
YearField.php

I copied the sample php file from the project's main page to test the project, here is the file (i.e. cron.php):

<?php
//require_once '/vendor/autoload.php'; //This line isn't valid because the mentioned path doesn't exist anymore. So I commented it but I can't find how to replace it.
// Works with predefined scheduling definitions
$cron = Cron\CronExpression::factory('@daily');
$cron->isDue();
echo $cron->getNextRunDate()->format('Y-m-d H:i:s');
echo $cron->getPreviousRunDate()->format('Y-m-d H:i:s');

// Works with complex expressions
$cron = Cron\CronExpression::factory('3-59/15 2,6-12 */15 1 2-5');
echo $cron->getNextRunDate()->format('Y-m-d H:i:s');

// Calculate a run date two iterations into the future
$cron = Cron\CronExpression::factory('@daily');
echo $cron->getNextRunDate(null, 2)->format('Y-m-d H:i:s');

// Calculate a run date relative to a specific time
$cron = Cron\CronExpression::factory('@monthly');
echo $cron->getNextRunDate('2010-01-12 00:00:00')->format('Y-m-d H:i:s');
?>

So my file "cron.php" exists in the same folder as the directory "Cron" which contains the project files listed earlier. When I execute my php file php cron.php, I get the following error:

[29-Apr-2013 15:17:23 UTC] PHP Fatal error:  Class 'Cron\CronExpression' not found in E:\PHP\Libraries\Cron\cron.php on line 3

What am I doing wrong here ?!

Upvotes: 0

Views: 962

Answers (2)

pilsetnieks
pilsetnieks

Reputation: 10420

It's the line you commented out as invalid:

//require_once '/vendor/autoload.php'; //This line isn't valid because the mentioned path doesn't exist anymore. So I commented it but I can't find how to replace it.

You need to install this package with Composer, then it will generate the /vendor/autoload.php script for you automatically.

Alternatively, you can just require_once every file in the src directory in your own code but take care to look at the order in which the files should be required because some of them extend classes that are defined in other files, so those other files with the base classes should be required first.

Upvotes: 1

fullybaked
fullybaked

Reputation: 4127

If you aren't using the autoloader you will need

require 'Cron/CronExpression.php';

at the top of the code. At the moment, there is no way for the code you posted to know where to find the CronExpression class in order to instantiate it

Upvotes: 0

Related Questions