Question

I want to extend, not just create a new instance of a class I have sitting in my vendors directory. I googled and read the docs but I see no support for it.

Can I do an app import of the 3rd party class, then write up the extended class followed by a component that will use my child class?

i.e

/* vendors/yahooapi/yahoo.class.php */
class YahooAPI {
     var $key = 'demo';
}

/* controllers/components/yahoo.php */
App::import("Vendor", "YahooAPI", array("file"=>"yahooapi.class.php"));

class Yahoov2 extends YahooAPI {
     var $key = 'newKey';
     function go() {}
}

YahooComponent extends Object {
     function goFaster() {
         $a = new Yahoov2;
         return $a->go() * 2;
     } 

}

Was it helpful?

Solution

Basically, I will tell you how I would do it (at least I've did it in some projects):

1 add your vendor vendors/yahooapi/yahoo.class.php as you did

2 create a file inside the vendors/yahooapi/ or outside in vendors/ which will extend the original vendor class let's say vendors/yahoov2.php i.e.

include_once('.../vendors/yahooapi/yahoo.class.php');
class Yahoov2 extends YahooAPI {
 var $key = 'newKey';
 function go() {}
}

3 And finally include in the component your extension as you did in your controller.

I believe that also extending the class in your controller directly would do the job, but it's really a matter of taste.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top