Results 1 to 8 of 8

Thread: new()

  1. #1
    Join Date
    Jan 2012
    Posts
    52
    Thanks
    17
    Thanked 0 Times in 0 Posts

    Default new()

    what is the use of new() in php?

  2. #2
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    I don't believe that new() is a function in PHP. It is not listed on php.net either. Can you give more information about what you're interested in? Where did you find this information? Or do you have some code you want to understand?

    new() with () is a function. But new could be a command (not a function)-- maybe you are thinking about that.

    In Object Oriented Programming in PHP, with classes, you can create a new instance of a class with the command new.

    See this very basic example:
    PHP Code:
    <?php
    $john 
    = new Employee();
    $sally = new Employee();
    ?>
    Employee() is a class defined somewhere else. new creates an instance of an "employee" (from the Employee() class).

    For more information: http://www.php.net/manual/en/language.oop5.php
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  3. The Following User Says Thank You to djr33 For This Useful Post:

    megha_3000 (01-25-2012)

  4. #3
    Join Date
    Jan 2012
    Posts
    52
    Thanks
    17
    Thanked 0 Times in 0 Posts

    Default

    i got an example of setting up a class that is

    Code:
    <?php  
    class siteCopyright { 
        // simple function inside my class (function getCopyrightYear() is a method of siteCopyright) 
        function getCopyrightYear() { 
             
           $var1 = "&copy;"; 
           $var2 = date("Y");     
           $var3 = "Flash Building"; 
            
           echo "<h2>$var1$var2 $var3</h2>"; 
            
        } 
         
    } 
    // Set new instance of the class, creating an object with that class's properties 
    $myFirstObject = new siteCopyright(); 
    // Now execute the function defined inside our class 
    $myFirstObject -> getCopyrightYear();          
    ?>
    its clear but i wanted to be more clear why new instance is used in here. though i m new in php programming so i am a little bit confused.
    However thanks 4 ur promt reply. if u discuss a little more than i will be greatful.
    Last edited by keyboard; 10-01-2012 at 10:05 PM. Reason: Format: [code][/code] Tags

  5. #4
    Join Date
    Jul 2010
    Location
    Minnesota
    Posts
    256
    Thanks
    1
    Thanked 21 Times in 21 Posts

    Default

    Well Daniel already explained the basics of the new class, but if you want more info check out the OOP tutorials on this youtube channel - http://youtube.com/betterphp

  6. The Following User Says Thank You to fastsol1 For This Useful Post:

    megha_3000 (01-26-2012)

  7. #5
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    Objects are like "things". You use new to create an instance of those things.

    So with my employee examples above, you can create an instance (stored in a variable) for every employee.

    For you, you're creating copyright objects.

    What is useful about them is that you can modify them or execute functions in them (called methods in OOP).

    But just look at some general tutorials until you understand. It's a significantly different way to program in PHP, but it's very useful.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  8. The Following User Says Thank You to djr33 For This Useful Post:

    megha_3000 (01-26-2012)

  9. #6
    Join Date
    Jan 2012
    Posts
    52
    Thanks
    17
    Thanked 0 Times in 0 Posts

    Default

    Thanks fastsol1 and thanks daniel. Well fastsol1 may i can have some tutorials of html & css. Like these php tutorials. Though i didn't see it yet.But I think i got helpful by these tutorials.

  10. #7
    Join Date
    Jul 2010
    Location
    Minnesota
    Posts
    256
    Thanks
    1
    Thanked 21 Times in 21 Posts

    Default

    You'll need to search around for html & css tutorials. The channels I surf only deal with php.

  11. #8
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    best HTML+CSS+JavaScript tutorials I've come across are Google's "From the Ground Up" series.

  12. The Following User Says Thank You to traq For This Useful Post:

    megha_3000 (01-28-2012)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •