Log in

View Full Version : new()



megha_3000
01-23-2012, 06:46 AM
what is the use of new() in php?

djr33
01-23-2012, 07:34 AM
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
$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

megha_3000
01-25-2012, 11:44 AM
i got an example of setting up a class that is


<?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.

fastsol1
01-25-2012, 06:48 PM
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

djr33
01-26-2012, 03:01 AM
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.

megha_3000
01-26-2012, 05:17 AM
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.

fastsol1
01-26-2012, 04:18 PM
You'll need to search around for html & css tutorials. The channels I surf only deal with php.

traq
01-26-2012, 09:11 PM
best HTML+CSS+JavaScript tutorials I've come across are Google's "From the Ground Up (http://code.google.com/edu/submissions/html-css-javascript/)" series.