Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extending Core Classes #358

Closed
JakeAi opened this issue Jan 9, 2017 · 3 comments
Closed

Extending Core Classes #358

JakeAi opened this issue Jan 9, 2017 · 3 comments

Comments

@JakeAi
Copy link
Contributor

JakeAi commented Jan 9, 2017

Coming from CI3 it was easier to edit functionality to the core. I have asked a buddy and have been messing with it myself but we can't seem to edit core classes without some hacky workarounds. According to the documentation it doesn't work as described? Maybe I am blind but this is what I have. I am trying to modify how this appends the code, but I can't get the function to override the original. Am I missing something?

  $classmap = [
          'CodeIgniter\\HTTP\\Message' => APPPATH . '/Libraries/Message.php',
      ];
<?php

//namespace CodeIgniter\HTTP;

class Message extends \CodeIgniter\HTTP\Message {

   public function appendBody($data) {
      $this->body .= (string) $data;

      return 'Does this work?';
   }

}
@lonnieezell
Copy link
Member

The Message class is a little bit of a different beast, honestly, since the Request and Response classes directly extend that class. If you're trying to add this method to the Response class, then you'd create a new class that extends CodeIgniter\HTTP\Response, we'll just add it to our application folder directly for now:

<?php namespace App;

class Response extends \CodeIgniter\HTTP\Response 
{
    public function appendBody(string $data)
    {
        $this->body .= $data;
        return $this;
    }
}

Then you'd modify the Service the system uses to use the new class instead of the existing one. The best way to do this is to copy the response() method from system/Config/Services.php to application/Config/Services.php and modify it to use the new class:

public static function response(\Config\App $config = null, $getShared = true)
	{
		if ($getShared)
		{
			return self::getSharedInstance('response', $config);
		}

		if (! is_object($config))
		{
			$config = new \Config\App();
		}

		return new \App\Response($config);
	}

And that's done. Now, when the system starts up, it should use your new class in place of the built-in one during all Response interactions throughout your application.

No need to mess with the ClassMap directly in most cases, at least not when you have a namespaced class the autoloader can find.

@lonnieezell
Copy link
Member

Oh - and next time, please use the forums to ask questions. This should be for bug reports only.

@JakeAi
Copy link
Contributor Author

JakeAi commented Jan 9, 2017

Ahh, perfect! It works. This makes a lot more sense and is exactly what i needed to keep extending. Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants