<p>I am trying to create a Sub Task with Craft 2 that uses a task class. I have created some generic code to explain my setup:</p>

<p>Parent Task Class:</p>

<pre><code>&lt;?php

namespace Craft;

class Downloadxml_ParentTask extends BaseTask{
    //typical defineSettings(), getDescription(), etc

    public function runStep(){
        craft()-&gt;downloadxml_fooCalc-&gt;getFooCalc();
        return true;    
     }
}
</code></pre>

<p>Sub Task Class</p>

<pre><code>&lt;?php

namespace Craft;

class Downloadxml_ChildTask extends Downloadxml_ParentTask{

    //No defineSettings(), getDescription(), etc 
    //since we inherit from parent class

    public function runStep(){
        craft()-&gt;downloadxml_barCalc-&gt;getBarCalc();
        return true;
    }

}
</code></pre>

<p>Related Main Task runner:</p>

<pre><code>&lt;?php

namespace Craft;

class Downloadxml_GetAllXmlTask extends BaseTask{
//typical defineSettings(), getDescription(), etc
    public function runStep(){
    $settings = array(
        'foo' =&gt; 'yub',
        'bar' =&gt; 'nub',
    );
    $message = 'Preparing Bar for Calc';
    return $this-&gt;runSubTask('Downloadxml_Child' $message, $settings);
    }
}
</code></pre>

<p>Calling the task to run from Console Command:</p>

<pre><code>&lt;?php
namespace Craft;

class DownloadxmlCommand extends BaseCommand
{

    public function actionGenerateXMLForDownload() 
    {
        craft()-&gt;downloadxml_taskCreateService-&gt;createDownloadTask();
        craft()-&gt;downloadxml_taskCreateService-&gt;runAllTasks();
    }
}
</code></pre>

<p>The plugin service the Command is calling:</p>

<pre><code>&lt;?php
namespace Craft;

class Downloadxml_TaskCreateService extends BaseApplicationComponent
{
    public function createDownloadTask()
    {
        $message = "Starting XML Download";
        $settings = array();
        craft()-&gt;tasks-&gt;createTask('Downloadxml_GetAllXml', $message, $settings);
    }
    public function runAllTasks() {

        // Run tasks
        if (!craft()-&gt;tasks-&gt;isTaskRunning())
        {
            $task = craft()-&gt;tasks-&gt;getNextPendingTask();

            if ($task)
            {
                craft()-&gt;tasks-&gt;runPendingTasks();
            }
        }
    }
}
</code></pre>

<p>When I run the <code>Downloadxml_GetAllXmlTask</code> task, I get an error of:</p>

<p><code>Fatal error: Class 'Craft\Downloadxml_ParentTask' not found in &lt;location of Main Task Runner&gt;</code></p>

<p>The reason I am doing this is to try and keep my codebase DRY as to not have the same code for the settings for each task class. I have several classes like this, and they are failing the same way. Looking through core, it doesn't immediately appear clear to me as to why this isn't inheriting properly and working as expected. Currently running Craft 2.6.2967.</p>

CC BY-SA 4.0
2018-07-26T21:56:23.143Z
Try using `return $this->runSubTask('DownloadxmlChild' $message, $settings);` (no `Task` at the end of the string)
Brad Bell
CC BY-SA 4.0
2018-07-27T12:15:56.640Z
Sorry, bad copy and paste. I am running it without the Task at the end. I will edit my code above to be correct. Any other thoughts on what this might be?
Brian McCaul
CC BY-SA 4.0
2018-07-27T13:51:51.583Z
Craft won't autoload plugin tasks by default. Are you loading those task classes elsewhere in your plugin (like in init())?
Brad Bell
CC BY-SA 4.0
2018-07-27T15:26:16.383Z
I have added additional code so you can see how we are calling and creating the tasks. If the `Downloadxml_ChildTask` class extends the `BaseTask` class, everything works as expected (if I add the required `defineSettings` and other functions).
Brian McCaul
CC BY-SA 4.0
2018-07-27T16:34:11.203Z
Sorry... what I mean is are you doing a `require_once('path/to/Downloadxml_ParentTask.php');` or `Craft::import('path.to.Downloadxml_ParentTask');` somewhere else in your plugin (like in init()), so that the class is loaded into memory?
Brad Bell
CC BY-SA 4.0
2018-07-27T20:20:08.750Z
While this is no longer giving me a fatal error, it does not appear to be actually be inheriting anything from the `Downloadxml_ParentTask` task. It is almost like the `runSubTask` method doesn't fully instantiate a new class from what is passed into it. Looking at the source for `runSubTask`, it appears to be wrapper for `createTask` and set the type for the newly created task to the string passed in runSubTask.
Brian McCaul
<plugin-development><plugins><tasks>
Subclassing BaseTask Class
# Subclassing BaseTask Class

(Asked by: username_0 on 2018-07-26)

I am trying to create a Sub Task with Craft 2 that uses a task class. I have created some generic code to explain my setup:
Parent Task Class:
downloadxml_fooCalc->getFooCalc();
        return true;    
     }
}

Sub Task Class
downloadxml_barCalc->getBarCalc();
        return true;
    }

}

Related Main Task runner:
 'yub',
        'bar' => 'nub',
    );
    $message = 'Preparing Bar for Calc';
    return $this->runSubTask('Downloadxml_Child' $message, $settings);
    }
}

Calling the task to run from Console Command:
downloadxml_taskCreateService->createDownloadTask();
        craft()->downloadxml_taskCreateService->runAllTasks();
    }
}

The plugin service the Command is calling:
tasks->createTask('Downloadxml_GetAllXml', $message, $settings);
    }
    public function runAllTasks() {

        // Run tasks
        if (!craft()->tasks->isTaskRunning())
        {
            $task = craft()->tasks->getNextPendingTask();

            if ($task)
            {
                craft()->tasks->runPendingTasks();
            }
        }
    }
}

When I run the Downloadxml_GetAllXmlTask task, I get an error of:
Fatal error: Class 'Craft\Downloadxml_ParentTask' not found in 
The reason I am doing this is to try and keep my codebase DRY as to not have the same code for the settings for each task class. I have several classes like this, and they are failing the same way. Looking through core, it doesn't immediately appear clear to me as to why this isn't inheriting properly and working as expected. Currently running Craft 2.6.2967.

**Tags:** plugin-development, plugins, tasks
> **Comments:**
> **username_1**: Try using `return $this->runSubTask('DownloadxmlChild' $message, $settings);` (no `Task` at the end of the string)  
> **username_0**: Sorry, bad copy and paste. I am running it without the Task at the end. I will edit my code above to be correct. Any other thoughts on what this might be?  
> **username_1**: Craft won't autoload plugin tasks by default. Are you loading those task classes elsewhere in your plugin (like in init())?  
> **username_0**: I have added additional code so you can see how we are calling and creating the tasks. If the `Downloadxml_ChildTask` class extends the `BaseTask` class, everything works as expected (if I add the required `defineSettings` and other functions).  
> **username_1**: Sorry... what I mean is are you doing a `require_once('path/to/Downloadxml_ParentTask.php');` or `Craft::import('path.to.Downloadxml_ParentTask');` somewhere else in your plugin (like in init()), so that the class is loaded into memory?  
> **username_0**: While this is no longer giving me a fatal error, it does not appear to be actually be inheriting anything from the `Downloadxml_ParentTask` task. It is almost like the `runSubTask` method doesn't fully instantiate a new class from what is passed into it. Looking at the source for `runSubTask`, it appears to be wrapper for `createTask` and set the type for the newly created task to the string passed in runSubTask.  

---

## 0 answers

