Jump to content

Archived

This topic is now archived and is closed to further replies.

HGeneAnthony

How can I create a class instance from another class

Recommended Posts

I was wondering how can I create an instance of my class inside another class? Example:

[code]
    class Auction {
        require("search.php");
        var $search = new Search();
        $search->setAuctionMonth(05);
        $search->setAuctionDay(15);
        $search->setAuctionYear(2006);
        $search->setOrderBy("");
        $search->setMake("ford");
        $search->setModel("");
        $search->setYearLow("");
        $search->setYearHigh("");
        $search->setMilesLow("");
        $search->setMilesHigh("");
    }
[/code]

This shoots out an error. How can I write this so I can call my other class from inside my current one.

Share this post


Link to post
Share on other sites
I don't think you can do that. But you can use the statement [i]extends[/i] statement to "reuse" a class:

[a href=\"http://www.php.net/manual/en/language.oop.php\" target=\"_blank\"]http://www.php.net/manual/en/language.oop.php[/a]
[a href=\"http://www.php.net/manual/en/keyword.extends.php\" target=\"_blank\"]http://www.php.net/manual/en/keyword.extends.php[/a]

Share this post


Link to post
Share on other sites
set the require statement outside of the class, at the top of the page
then just call it like you would any other Class


[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]But you can use the statement extends statement to "reuse" a class:[/quote]
not a good idea
you might not want one class to have the properties of another
only use extends when you need to .....literally extend from it

for instance if you made a Car class
and you decided to make a Motorcycle class also....
then you could logically EXTEND it onto CAR, maybe

Share this post


Link to post
Share on other sites
[!--quoteo(post=378324:date=May 29 2006, 11:58 PM:name=zanus)--][div class=\'quotetop\']QUOTE(zanus @ May 29 2006, 11:58 PM) [snapback]378324[/snapback][/div][div class=\'quotemain\'][!--quotec--]
set the require statement outside of the class, at the top of the page
then just call it like you would any other Class
not a good idea[/quote]
zanus, maybe I am doing it wrong, but no matter where you put the require statement, once you try to initiate the class it will throw you an error like this:

[b]Parse error: parse error, unexpected T_NEW in...[/b]

Share this post


Link to post
Share on other sites
Well I found out that the require statement must be outside of a class (at the to preferably). I found out you can create have an instance of a class inside a class however you need to specify the variable at the top of the class:

var $cars;

and instantiate it inside a method:

$cars = new Cars();

This would work!

Extending a class is generally only used for an abstract class like human and dog would extend mammal. You would put all the common characteristics between humans and dogs inside of mammal and then extend the human and dog off of them. You can even override characteristics of mammal inside of human or dog. This keeps your code cleanier (hence less error prone) and builds on a modular design which is more better for designing any project. Another use for extending classes is so that you don't have to modify a very modular class with project specific code. You design classes that can be dumped in everything and then create an extended class on top of it to modify anything specific to your project.

Share this post


Link to post
Share on other sites
You can use the require() within a class, however, it also needs to be within a method. eg;
[code]
class Auction {
    function createSearch() {
        require "search.php";
        return new Search();
    }
}
[/code]

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.