Jump to content

How can I create a class instance from another class


HGeneAnthony

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.
Link to comment
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]
Link to comment
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
Link to comment
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]
Link to comment
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.
Link to comment
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]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.