Reputation: 7165
Consider the following:
I have a class that does some mild reporting,
example 1
<?php
namespace App\Log;
class Reporting
{
private $config;
function __construct($config)
{
$this->config = $config;
}
public function ReportSomething($action)
{
// blah blah, sends data to a server
}
}
?>
Then I also class where I want to use the Reporting class multiple times:
example 2
<?php
namespace App\Controller
use App\Log\Reporting;
class DoStuff
{
public function getUser()
{
// blah blah, business logic
$reporting = new Reporting($config);
$reporting->ReportSomething('going to db');
$reporting->ReportSomething('got data');
$reporting2 = new Reporting($newConfig);
$reporting2->ReportSomething('preparing to send');
$reporting2->ReportSomething('sent');
}
public function getPosts()
{
// blah blah, business logic
$reporting = new Reporting($config);
$reporting->ReportSomething('going to db');
$reporting->ReportSomething('got data');
$reporting2 = new Reporting($newConfig);
$reporting2->ReportSomething('preparing to send');
$reporting2->ReportSomething('sent');
}
/* you get the point */
}
?>
My question is: performance wise, what would be the impact if I were to use this type of style instead:
example 3
<?php
namespace App\Controller
class DoStuff
{
public function getUser()
{
// blah blah, business logic
$reporting = new \App\Log\Reporting($config);
$reporting->ReportSomething('going to db');
$reporting->ReportSomething('got data');
$reporting2 = new \App\Log\Reporting($newConfig);
$reporting2->ReportSomething('preparing to send');
$reporting2->ReportSomething('sent');
}
public function getPosts()
{
// blah blah, business logic
$reporting = new \App\Log\Reporting($config);
$reporting->ReportSomething('going to db');
$reporting->ReportSomething('got data');
$reporting2 = new \App\Log\Reporting($newConfig);
$reporting2->ReportSomething('preparing to send');
$reporting2->ReportSomething('sent');
}
/* you get the point */
}
?>
Where, as you can see, example 3 no longer uses the use
keyword to import, but rather does new \App\Log\Reporting
each time it wants to obtain a new instance of the Reporting
class.
Are there any downsides to either use cases? Is one better (based on some data or information) than another, and why?
Upvotes: 1
Views: 99
Reputation: 1295
use
and namespace
don't actually access the class, they simply store the namespace or class as a string so they will run at relatively similar speeds. Given that I would go for example 2 since it encourages the organizational structure thats offered by using those keywords.
Upvotes: 3