views:

37

answers:

1

I want to extended, 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;
     } 

}

+1  A: 

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.

Nik
If you can get `App::import` working for your file then great, otherwise - as deceze points out - you can always fall back on plain PHP as shown in Nik's example here.
deizel
I would agree that App::import is more Cake style, but I am not sure that App::import would work in Vandors.What I did in the past is to do App::import('Vendor', 'core_vendor');App::import('Vendor', 'ext_vendor');In the component. It's really a matter of taste :)
Nik
By the time the vendor file is being loaded, the `App` class has long been loaded and can be used anywhere. I'd go with `App::import` whenever possible, since it leaves you a little more flexibility when moving files around.
deceze
thanks nik,deizel, deceze
Angel S. Moreno