[QUOTE=Cow Muffins;34281001]Thanks much![/QUOTE]
Make sure not to overdo them.
[QUOTE=h2ooooooo;34280842]Working on a PHP framework
Here's the errors/debug messages it produces:
[img]http://www.jalsoedesign.net/zscreen/ptas_1.png[/img]
Of course complete with a stack trace when you click:
[img]http://www.jalsoedesign.net/zscreen/ptas_2.png[/img]
[editline]18th January 2012[/editline]
The code for this in particular is the following:
[php]
<?php
namespace pTas;
require_once('ptas.php');
Debug::SetDebugLevel(DebugLevel::VERBOSE);
Core::IncludeModuleOnce('Curl');
Core::IncludeModuleOnce('Crap');
Curl::CreateInstance('derp', true);
Curl::CreateInstance('sherp');
Curl::SetInstance('sherp');
Curl::DeleteInstance('sherp');
Curl::SetInstance('sherp');
Debug::Error('Here\'s an "Error" debug message');
Debug::Warning('Here\'s a "Warning" debug message');
Debug::Info('Here\'s an "Info" debug message');
Debug::Debug('Here\'s a "Debug" debug message');
Debug::Verbose('Here\'s a "Verbose" debug message');
?>
[/php][/QUOTE]
My god...so many static calls...that looks like dependency hell - what if you want to inject your own class for something like Debug?
Pro-tip: 99.99% of the times you think a static method is the right thing to do, you'll be wrong.
[QUOTE=swift and shift;34234226]Holy fuck, go to [url]http://acko.net[/url] in Safari. That's amazing!
[img]http://i.imgur.com/9UTPj.png[/img]
(scroll down)[/QUOTE]
They have actually gone dark for the protest again sopa, that's pretty cool.
[QUOTE=StinkyJoe;34281442]My god...so many static calls...that looks like dependency hell - what if you want to inject your own class for something like Debug?
Pro-tip: 99.99% of the times you think a static method is the right thing to do, you'll be wrong.[/QUOTE]
and yet here i was, thinking that it would be bad to use the same amount of instancizing
[QUOTE=TerabyteS_;34281094]Make sure not to overdo them.[/QUOTE]
i.e. one per page (IMO--I'm not a big fan of them to begin with)
It has been a while since I've posted on Facepunch; I've been working on an interface for a client management system that I've been developing.
[img]http://katanaproject.com/screens/1.png[/img]
[img]http://i.imgur.com/bGEOQ.png[/img]
[QUOTE=adamjon858;34285693][img]http://i.imgur.com/bGEOQ.png[/img][/QUOTE]
Why do you use such strong perspective distortions on the text? It'd look better without imo.
This is something I worked on few years back, thought about finishing it and translating it to English.
[img_thumb]http://rulssi.kapsi.fi/crap/matkailu.jpg[/img_thumb]
[QUOTE=h2ooooooo;34280842]Working on a PHP framework
Here's the errors/debug messages it produces:
[img]http://www.jalsoedesign.net/zscreen/ptas_1.png[/img]
Of course complete with a stack trace when you click:
[img]http://www.jalsoedesign.net/zscreen/ptas_2.png[/img]
[/QUOTE]
To the guys who rated me disagree - please come with your input. What would you suppose that I do instead? I've been going over this in my head a million times, and static classes was the closest I got to a global solution without the need for regular classes and or creating/getting instances first.
Rate your favourite one:
[b]Current:[/b] [img]http://www.facepunch.com/fp/ratings/tick.png[/img]
[php]
<?php
namespace pTas;
require_once('ptas.php');
Debug::SetDebugLevel(DebugLevel::VERBOSE);
Core::IncludeModuleOnce('Curl');
Core::IncludeModuleOnce('Crap');
Curl::CreateInstance('derp', true);
Curl::CreateInstance('sherp');
Curl::SetInstance('sherp');
Curl::DeleteInstance('sherp');
Curl::SetInstance('sherp');
Debug::Error('Here\'s an "Error" debug message');
Debug::Warning('Here\'s a "Warning" debug message');
Debug::Info('Here\'s an "Info" debug message');
Debug::Debug('Here\'s a "Debug" debug message');
Debug::Verbose('Here\'s a "Verbose" debug message');
?>
[/php]
[b]Singleton:[/b] (even though it [url=http://moisadoru.wordpress.com/2010/03/02/static-call-versus-singleton-call-in-php/][i]might[/i] be slower in a big environment[/url]) [img]http://www.facepunch.com/fp/ratings/cross.png[/img]
[php]
<?php
namespace pTas;
require_once('ptas.php');
$debugInstance = Debug::GetInstance();
$debugInstance->SetDebugLevel(DebugLevel::VERBOSE);
$coreInstance = Core::GetInstance();
$coreInstance->IncludeModuleOnce('Curl');
$coreInstance->IncludeModuleOnce('Crap');
$curlInstance = Curl::GetInstance();
$curlInstance->CreateInstance('derp', true);
$curlInstance->CreateInstance('sherp');
$curlInstance->SetInstance('sherp');
$curlInstance->DeleteInstance('sherp');
$curlInstance->SetInstance('sherp');
$debugInstance->Error('Here\'s an "Error" debug message');
$debugInstance->Warning('Here\'s a "Warning" debug message');
$debugInstance->Info('Here\'s an "Info" debug message');
$debugInstance->Debug('Here\'s a "Debug" debug message');
$debugInstance->Verbose('Here\'s a "Verbose" debug message');
?>
[/php]
[b]Referencing by core:[/b] [img]http://www.facepunch.com/fp/ratings/funny2.png[/img]
[php]
<?php
namespace pTas;
require_once('ptas.php');
$core = Core::GetInstance();
$core->Debug->SetDebugLevel(DebugLevel::VERBOSE);
$core->IncludeModuleOnce('Curl');
$core->IncludeModuleOnce('Crap');
$core->Curl->CreateInstance('derp', true);
$core->Curl->CreateInstance('sherp');
$core->Curl->SetInstance('sherp');
$core->Curl->DeleteInstance('sherp');
$core->Curl->SetInstance('sherp');
$core->Debug->Error('Here\'s an "Error" debug message');
$core->Debug->Warning('Here\'s a "Warning" debug message');
$core->Debug->Info('Here\'s an "Info" debug message');
$core->Debug->Debug('Here\'s a "Debug" debug message');
$core->Debug->Verbose('Here\'s a "Verbose" debug message');
?>
[/php]
[b]Global variables #1 (yuck):[/b] [img]http://www.facepunch.com/fp/ratings/winner.png[/img]
[php]
<?php
namespace pTas;
require_once('ptas.php');
$GLOBALS["core"]->Debug->SetDebugLevel(DebugLevel::VERBOSE);
$GLOBALS["core"]->IncludeModuleOnce('Curl');
$GLOBALS["core"]->IncludeModuleOnce('Crap');
$GLOBALS["core"]->Curl->CreateInstance('derp', true);
$GLOBALS["core"]->Curl->CreateInstance('sherp');
$GLOBALS["core"]->Curl->SetInstance('sherp');
$GLOBALS["core"]->Curl->DeleteInstance('sherp');
$GLOBALS["core"]->Curl->SetInstance('sherp');
$GLOBALS["core"]->Debug->Error('Here\'s an "Error" debug message');
$GLOBALS["core"]->Debug->Warning('Here\'s a "Warning" debug message');
$GLOBALS["core"]->Debug->Info('Here\'s an "Info" debug message');
$GLOBALS["core"]->Debug->Debug('Here\'s a "Debug" debug message');
$GLOBALS["core"]->Debug->Verbose('Here\'s a "Verbose" debug message');
?>
[/php]
[b]Global variables #2 (yuck again):[/b] [img]http://www.facepunch.com/fp/ratings/zing.png[/img]
[php]
<?php
namespace pTas;
require_once('ptas.php');
$GLOBALS["debug"]->SetDebugLevel(DebugLevel::VERBOSE);
$GLOBALS["core"]->IncludeModuleOnce('Curl');
$GLOBALS["core"]->IncludeModuleOnce('Crap');
$GLOBALS["curl"]->CreateInstance('derp', true);
$GLOBALS["curl"]->CreateInstance('sherp');
$GLOBALS["curl"]->SetInstance('sherp');
$GLOBALS["curl"]->DeleteInstance('sherp');
$GLOBALS["curl"]->SetInstance('sherp');
$GLOBALS["debug"]->Error('Here\'s an "Error" debug message');
$GLOBALS["debug"]->Warning('Here\'s a "Warning" debug message');
$GLOBALS["debug"]->Info('Here\'s an "Info" debug message');
$GLOBALS["debug"]->Debug('Here\'s a "Debug" debug message');
$GLOBALS["debug"]->Verbose('Here\'s a "Verbose" debug message');
?>
[/php]
[b]Regular old (possible re)creating of classes:[/b] [img]http://www.facepunch.com/fp/ratings/information.png[/img]
[php]
<?php
namespace pTas;
require_once('ptas.php');
$debugClass = new Debug();
$debugClass->SetDebugLevel(DebugLevel::VERBOSE);
$coreClass->IncludeModuleOnce('Curl');
$coreClass->IncludeModuleOnce('Crap');
$curlClass = new Curl();
$curlClass->CreateInstance('derp', true);
$curlClass->CreateInstance('sherp');
$curlClass->SetInstance('sherp');
$curlClass->DeleteInstance('sherp');
$curlClass->SetInstance('sherp');
$debugClass->Error('Here\'s an "Error" debug message');
$debugClass->Warning('Here\'s a "Warning" debug message');
$debugClass->Info('Here\'s an "Info" debug message');
$debugClass->Debug('Here\'s a "Debug" debug message');
$debugClass->Verbose('Here\'s a "Verbose" debug message');
?>
[/php]
Why do you need to log so much shit anyway?
[QUOTE=Jelly;34291463]Why do you need to log so much shit anyway?[/QUOTE]
I don't, it was to test the module. Hence the test message, and the line "SetDebugLevel(DebugLevel::VERBOSE)". Default is simply ERROR and WARNING, and therefore wouldn't output (not log), all this "shit".
[editline]19th January 2012[/editline]
[QUOTE=StinkyJoe;34281442]My god...so many static calls...that looks like dependency hell - what if you want to inject your own class for something like Debug?
Pro-tip: 99.99% of the times you think a static method is the right thing to do, you'll be wrong.[/QUOTE]
Do you mean using the debug class inside of your own class? Very simple. Just call Debug (if in pTas namespace) or pTas\Debug. The reason for the staticness is global access without the use of singletons. The core automatically imports stuff itself when you call IncludeModule or IncludeModuleOnce, and once it's included, it ignores it if you try (for some odd reason) to include it again - hence working the same way as singletons, as there's no need for additional overhead by creating a class multiple times.
[QUOTE=rulssi;34291386]This is something I worked on few years back, thought about finishing it and translating it to English.
[img_thumb]http://rulssi.kapsi.fi/crap/matkailu.jpg[/img_thumb][/QUOTE]
Those colors.....please please tone them down. Way too obnoxious.
[editline]19th January 2012[/editline]
[QUOTE=h2ooooooo;34291426]To the guys who rated me disagree - please come with your input. What would you suppose that I do instead? I've been going over this in my head a million times, and static classes was the closest I got to a global solution without the need for regular classes and or creating/getting instances first.[/QUOTE]
Combination of Singleton and Core. Also, who's telling you that their inefficient? They're only inefficient if you're building massive massive architecture's without a starting framework. And usually there's going to be another weak link before you get to that point.
For any framework, Singletons are all but necessary to work effectively and efficiently.
[editline]19th January 2012[/editline]
[QUOTE=TerabyteS_;34288997]Why do you use such strong perspective distortions on the text? It'd look better without imo.[/QUOTE]
Huh? That's a real life picture. No photoshopping.
[QUOTE=adamjon858;34292942]
Huh? That's a real life picture. No photoshopping.[/QUOTE]
The text is shopped on.
[QUOTE=adamjon858;34292942]
Combination of Singleton and Core. Also, who's telling you that their inefficient? They're only inefficient if you're building massive massive architecture's without a starting framework. And usually there's going to be another weak link before you get to that point.
For any framework, Singletons are all but necessary to work effectively and efficiently.
[/QUOTE]
For every new function you start, if you want to reference the core or any module in it, you'll have to call Core::GetInstance() once again, and assign the reference to a variable - this seems like a waste of calls (if done in for example 100 functions spread across your classes/pages) compared to simply always being able to call the static function, which was the main reason I did it the other way to start with.
[editline]19th January 2012[/editline]
Take the following example:
[php]
<?php
function a() {
$core = Core::GetInstance();
$core->Debug->Debug("inside of function A");
b();
}
function b() {
$core = Core::GetInstance();
$core->Debug->Debug("inside of function B");
for ($i = 0; $i < 100; $i++) {
c($i);
}
}
function c($i) {
$core = Core::GetInstance();
$core->Debug->Debug("inside of function C with count " . $i);
}
?>
[/php]
That's 102 calls to GetInstance.
[editline]19th January 2012[/editline]
Compared to the following:
[php]
<?php
function a() {
Debug::Debug("inside of function A");
b();
}
function b() {
Debug::Debug("inside of function B");
for ($i = 0; $i < 100; $i++) {
c($i);
}
}
function c($i) {
Debug::Debug("inside of function C with count " . $i);
}
?>
[/php]
But again, I don't know what has the most overhead. My guess would be static methods, but I'm unsure.
Your idea of a singleton is wrong, really - there's absolutely NOTHING wrong with having your application create an instance, and using that instance only, that's NOT what a singleton is.
[php]
class Application
{
private $http_handler;
public function start()
{
$this->http_handler = new \http\Handler;
}
public function http()
{
return $this->http_handler;
}
}
$application = new Application;
$application->start();
$application->http()->end(200);
[/php]
That is NOT a singleton, and allows you to manage your dependencies in a sane and flexible manner (there's no ties to any particular class through static calls, there's no global state, etc).
Stop worrying about performance so much, you're basing yourself on near-voodoo concepts that are at best misguided, and worst completely incorrect.
[QUOTE=StinkyJoe;34297126]Your idea of a singleton is wrong, really - there's absolutely NOTHING wrong with having your application create an instance, and using that instance only, that's NOT what a singleton is.
[php]
class Application
{
private $http_handler;
public function start()
{
$this->http_handler = new \http\Handler;
}
public function http()
{
return $this->http_handler;
}
}
$application = new Application;
$application->start();
$application->http()->end(200);
[/php]
That is NOT a singleton, and allows you to manage your dependencies in a sane and flexible manner (there's no ties to any particular class through static calls, there's no global state, etc).
Stop worrying about performance so much, you're basing yourself on near-voodoo concepts that are at best misguided, and worst completely incorrect.[/QUOTE]
How would you then rewrite the code in my previous post using your method?
[editline]20th January 2012[/editline]
The only way that I could see that it would possibly work (without the use of global variables) is this:
[php]
<?php
class Application
{
private $http_handler;
public function start()
{
$this->http_handler = new \http\Handler;
}
public function http()
{
return $this->http_handler;
}
}
function a() {
$application = new Application;
$application->start();
$application->test()->blah("asd");
b();
}
function b() {
$application = new Application;
$application->start();
$application->test()->blah("asd #2");
for ($i = 0; $i < 100; $i++) {
c($i);
}
}
function c($i) {
$application = new Application;
$application->start();
$application->test()->blah("asd #3 - " $i);
}
?>
[/php]
Unless you want to make a new application class for every single other class you have?
[editline]20th January 2012[/editline]
If you have any other idea as to how it could be made, feel free to tell me. I'm always up for possibly learning something new. :)
Going to try and type this out on a smartphone, pardon any mistake...you're not understanding how object orientation works...your application is a "thing", it has its dependencies, its own classes and execution lifetime...you can have, theoretically speaking, multiple active applications at any moment, without any sort of globally shared state or knowledge of their surroundings, and that's great! Hence, and to try and apply this to your example, each application has its own debugger instance, and methods for working with this instance - maybe you pass the application object around, maybe you pass the debugger directly, it really depends on what you're doing - ...where was I...it's easy to lose my train of thought at 2 wpm...anyway...global shared state bad, static methods bad, hard dependencies bad, high cohesion and accountability good!
With this, I'm going to sleep, I'l answer any questions tomorrow, tata!
bluh
[php]
class Core {
private $classes = array();
static public $instance;
function __construct()
{
if( empty( $this->instance ) )
{
self::$instance =& $this;
}
else
// throw some error
}
function extend( $name, &$class ){
$this->classes[ $classes ] =& $class;
}
function _get( $name )
{
if( !empty( $this->classes[ $name ] ) )
{
return $this->classes[ $name ];
}
else return false;
}
static function instance()
{
return self::$instance;
}
}
function get_instance()
{
return Core::instance();
}
[/php]
and then have a class meant for extending
[php]
class extend {
function _get( $name )
{
return get_instance()->{ $name };
}
}
[/php]
and then extend the core
[php]
class debug {
function log( $value, $something )
{
echo $value, $something;
}
}
get_instance()->extend( "debug", new debug );
[/php]
[php]
class App extends extend {
function __construct()
{
$this->debug->log( "something", "something" );
}
}
[/php]
that's how i'd do it but it could be a bit shitty
There are architectural purposes for using a singleton. Print/log debugging by itself is not one of them.
[QUOTE=amcfaggot;34302370]There are architectural purposes for using a singleton. Print/log debugging by itself is not one of them.[/QUOTE]
Once again, the print/logging was just an example. But as a person who just (thought that he) learnt what a singleton is just a few days ago, could you explain it to me, instead of just saying "this is not the way to do it", considering I obviously missed the point of it? It's really not very helpful.
[editline]20th January 2012[/editline]
Ac!dl3ak, that method, while appreciated written out, is essentially no different from my "Referencing by core" example, except that you add methods differently, and use a global function that calls the static function.
singletons are rarely the right thing to do
[editline]20th January 2012[/editline]
if you feel you have a valid use case for a singleton, you need to seriously think about it
[editline]20th January 2012[/editline]
For example, there are a few singletons I've implemented in Twostroke and none of them store [i]any[/i] state.
They're just singletons for the sake of memory efficiency:
[img]http://i.imgur.com/Pdz3X.png[/img]
[editline]20th January 2012[/editline]
If you have a singleton that keeps state (whether directly or indirectly through I/O), you're [i]probably[/i] doing it wrong.
So your suggestion in the top post on this page that global variables are the way to go, wasn't a joke? AFAIC, global variables are bad practise, or have I got that wrong?
[editline]20th January 2012[/editline]
By the way, thanks for the explanation. :)
[QUOTE=h2ooooooo;34304526]AFAIC, global variables are bad practise, or have I got that wrong?[/QUOTE]
global variables are usually bad
PHP is different because the entire thing is reloaded every time someone makes a request so there's not much state to encapsulate or any of that proper bullshit.
It's not like a long running application framework where you have a request and response object that encapsulate individual client requests and shit. In PHP there's [b]the[/b] request where you're serving up [b]a[/b] response to [b]the[/b] client.
[QUOTE=spidersdesign;34275858]Yes the portfolio site is shitty. I have made a few sites for example [URL]http://www.ringwoodtfsr.co.uk/[/URL]
- they just aren't listed there, I don't maintain my portfolio site as I no longer accept web design jobs. I would just take it down but a few posts get a reasonable amount of traffic so I have left it.[/QUOTE]
Like every other 'design' you've ever made it all just borrows from designers much more talented than yourself. Seriously, I think Orman Clarke's handiwork has appeared in almost all your sites to date.
snip
[QUOTE=swift and shift;34306908]should i buy yaff.in or yiff.asia or something else?[/QUOTE]
If you don't buy yaff.in, I will!
[QUOTE=StinkyJoe;34306978]If you don't buy yaff.in, I will![/QUOTE]
you can contribute it to our little project
To be fair, I think you all are just way over thinking this issue and it's getting ridiculous.
I use singletons for 3 things. Template engine, database connection, and my auth/session handler.
These are things you are probably going to want persistent across most of the classes you use...although template engine could be debated and I use Smarty (prepares for OMG INEFFICIENT shitstorm). Sure you can find a way to keep passing this stuff between classes without singletons, but once you start dealing with really large projects it gets quite crazy.
I have a few sites that can handle 1000 simultaneous visitors on an Intel Atom server.... For me, I've found that using Nginx instead of Apache and making sure I optimize my queries and my overall data layout has way more effect than worrying about singletons.
Furthermore, here's how I usually handle singletons:
[php]
<?php
class wtfshit extends modbase {
private $db;
private $auth;
private $smarty;
function __construct(){
$db = PDO::get_instance();
$auth = auth::get_instance();
$smarty = smartyex::get_instance();
$this->permission_add('home','public');
}
function home(){
$news=$this->db->get_rows('SELECT * from news');
$this->smarty->assign('news',$news);
return $this->smarty->fetch('home.html');
}
}
?>
[/php]
[editline]20th January 2012[/editline]
[QUOTE=swift and shift;34304427]
If you have a singleton that keeps state (whether directly or indirectly through I/O), you're [i]probably[/i] doing it wrong.[/QUOTE]
I entirely disagree. Once you start having 4-5 different modules and multiple API libraries that all need to interact with a single database connection....singletons are the way to go. You can make it work without singletons but they exist for a reason. There are lots of times where people misuse them (like I think his original post demonstrates), but they do have a purpose. Sometimes you need persistence even if it is just for the mere milliseconds of a single page load. In PHP, It's practically the core of getting the parts of MVC to work with each other effectively. I wouldn't say this about other languages, I'm just saying that for PHP it's a good choice.
For Python, I like using Twisted and having everything persistent :D. Having database objects cached in the memory for a minute or so unless they are used again is so much better then having to reload everything for each instance and connection.
Sorry, you need to Log In to post a reply to this thread.