ludo1960
9 months ago
286
21
Laravel

Instantiate abstract class

Posted 9 months ago by ludo1960

Hi Guys,

After much reading and googling, hers's what's up: from https://github.com/spatie/crawler

Usage
The crawler can be instantiated like this

Crawler::create()
    ->setCrawlObserver(<class that extends \Spatie\Crawler\CrawlObserver>)
    ->startCrawling($url);
The argument passed to setCrawlObserver must be an object that extends the \Spatie\Crawler\CrawlObserver abstract class:

So in my CrawlerCommand.php I added:

<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;

use \Spatie\Crawler\CrawlObserver;


abstract class myClass extends CrawlObserver {

}

class CrawlerCommand extends Command
{

Then in my Handle Method I added:

    public function handle()
    {
        //code goes here
        $myObject = new myClass(); //Change myClass to Object
     
        $url = 'php.local.com/'; // Set to localhost site
        Crawler::create()
        ->setCrawlObserver($myObject)
        ->startCrawling($url);
    }

So when I run php artisan crawler:crawl I get the following error:

   Symfony\Component\Debug\Exception\FatalThrowableError  : Cannot instantiate abstract class App\Console\Commands\myClass

  at /var/www/test.local.com/app/Console/Commands/CrawlerCommand.php:56
    52|     public function handle()
    53|     {
    54| 
    55|         //code goes here
  > 56|         $myObject = new myClass(); //Change myClass to Object
    57|         //$myObject = new CrawlObserver(); //instantiate
    58|         $url = 'php.local.com/'; // Set to localhost site
    59|         Crawler::create()
    60|         ->setCrawlObserver($myObject)

  Exception trace:

  1   App\Console\Commands\CrawlerCommand::handle()
      /var/www/test.local.com/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php:29

  2   call_user_func_array([])
      /var/www/test.local.com/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php:29

Pretty sure i'm on the right track, but missing a fundamental step somewhere, any ideas guys?

Please sign in or create an account to participate in this conversation.