- PHP Basics
- Learn PHP
- PHP Comments
- PHP Data Types
- PHP Variables
- PHP Operators
- PHP echo
- PHP print
- PHP echo vs. print
- PHP if else
- PHP switch
- PHP for Loop
- PHP while Loop
- PHP do...while Loop
- PHP foreach Loop
- PHP break and continue
- PHP Arrays
- PHP print_r()
- PHP unset()
- PHP Strings
- PHP Functions
- PHP File Handling
- PHP File Handling
- PHP Open File
- PHP Create a File
- PHP Write to File
- PHP Read File
- PHP feof()
- PHP fgetc()
- PHP fgets()
- PHP Close File
- PHP Delete File
- PHP Append to File
- PHP Copy File
- PHP file_get_contents()
- PHP file_put_contents()
- PHP file_exists()
- PHP filesize()
- PHP Rename File
- PHP fseek()
- PHP ftell()
- PHP rewind()
- PHP disk_free_space()
- PHP disk_total_space()
- PHP Create Directory
- PHP Remove Directory
- PHP Get Files/Directories
- PHP Get filename
- PHP Get Path
- PHP filemtime()
- PHP file()
- PHP include()
- PHP require()
- PHP include() vs. require()
- PHP and MySQLi
- PHP and MySQLi
- PHP MySQLi Setup
- PHP MySQLi Create DB
- PHP MySQLi Create Table
- PHP MySQLi Connect to DB
- PHP MySQLi Insert Record
- PHP MySQLi Update Record
- PHP MySQLi Fetch Record
- PHP MySQLi Delete Record
- PHP MySQLi SignUp Page
- PHP MySQLi LogIn Page
- PHP MySQLi Store User Data
- PHP MySQLi Close Connection
- PHP Misc Topics
- PHP Object Oriented
- PHP new Keyword
- PHP Cookies
- PHP Sessions
- PHP Date and Time
- PHP GET vs. POST
- PHP File Upload
- PHP Image Processing
PHP new keyword: Create an Object from a Class
The PHP "new" keyword is used when we need to create a new object from a class. For example:
<?php class Student { public $name = "Lucas"; public $city = "Amsterdam"; } $obj = new Student(); echo "Name: $obj->name"; echo "<BR>"; echo "City: $obj->city"; ?>
This PHP code defines a class called "Student". The class has two properties, namely "$name" and "$city" which are both initialized to string values of "Lucas" and "Amsterdam" respectively. Then, an object of the class "Student" is created using the "new" keyword and assigned to the variable "$obj".
Finally, the code outputs the values of the object's properties using the "echo" statement. It displays the string "Name: " followed by the value of the "$name" property and then a line break ("<BR>"). After that, it displays the string "City: " followed by the value of the "$city" property.
The output of the above PHP example on the "new" keyword is shown in the snapshot given below:
In other words, we can say that the PHP "new" keyword creates class instances. PHP creates a class-specific object and returns a reference when the new keyword is used.
The above example can also be written in this way:
<?php class Student { public $name = "Lucas"; public $city = "Amsterdam"; } $obj = new Student(); echo "Name: ", $obj->name, "<BR>City: ", $obj->city; ?>
Or
<?php class Student { public $name = "Lucas"; public $city = "Amsterdam"; } $obj = new Student(); $x = $obj->name; $y = $obj->city; echo "Name: ", $x, "<BR>City: ", $y; ?>
In the above examples, using the statement:
$obj = new Student();
An object named $obj is created of type Student class.
Advantages of the "new" keyword in PHP
- The "new" keyword gives your code a lot of flexibility by allowing you to create multiple instances of the same class with different properties and values.
- The "new" keyword helps to create reusable code by enabling you to create and instantiate classes. Object-oriented programming (OOP) promotes code reuse.
- The "new" keyword facilitates the division of code into modular chunks that are simpler to comprehend, maintain, and troubleshoot.
- By encapsulating data and functionality within an object using the "new" keyword, naming conflicts can be avoided and the code can be made more secure.
Disadvantages of the "new" keyword in PHP
- When you create an instance of a class using the "new" keyword, your code becomes closely related to that class. As a result, any changes to the class might necessitate changes to all the code that depends on it, which can make your code less flexible and more difficult to maintain.
- Using the "new" keyword can make your code more challenging to test because it can be challenging to isolate and mock dependencies. This may result in more unstable and unreliable tests.
- PWhen compared to other ways of creating objects in PHP, using the "new" keyword to create a new instance of a class can be somewhat slow. Performance problems in high-traffic applications may result from this.
- Using dependency injection, a crucial technique for producing flexible and reusable code, can be challenging when the "new" keyword is used.
« Previous Tutorial Next Tutorial »