Nao

  • Dadman with a boy
  • Posts: 16,082
Context object?
« on December 23rd, 2012, 11:26 AM »
I'd also like to create a cx (context) object. Not with the usual 'we' prefix, I know, but the idea is to keep it very, very short, and 'we' is already taken for the system class, although we MIGHT be able to use 'we' instead and just have we::$user behave like if it is was $context['user'], or whatever.

The main problems are:
1/ Performance. $context is used in TONS of areas, including time-critical code, so it's going to be hard to tell people to use 'cx::$var' instead of '$context['var']' in these areas. So we'd need to keep having a global point to the array.
2/ Because of (1) and general laziness from devs who might have $context so deeply carved into their DNA, we could/should/might use $context =& we::$cx, or cx::$cx, or something, but that means we can't use cx::$var, but instead we::$cx['var'], which only saves one byte compared to $context['var']. We could go as far as we:$c['var'], but even then, it's a bit ugly and all anyway...
3/ Some people might argue that we could simply rename $context to $cx, and be done with it, accept globals and that's it. :P

I'm looking into other solutions... So far I've found a strange one, which could work but only for variables that never change...
$context = get_object_vars(cx::getInstance());

This will effectively transform cx::$var into $context['var']. Seriously. But I'm guessing that, even without benchmarking it, this function call is not 'free' and thus can only be done on purpose at one point or another...

Ahhhhhhh... If only accessing a singleton variable was just as fast as accessing a global var! Why does it have to be about 60% slower..?!

Arantor

  • As powerful as possible, as complex as necessary.
  • Posts: 14,278
Re: Context object?
« Reply #1, on December 23rd, 2012, 01:38 PM »
Because of the fact you're addressing an object statically rather than actually addressing a variable. It is much the same overhead as calling a function - classes live in the same sort of place as functions and addressing is handled in much the same way.
When we unite against a common enemy that attacks our ethos, it nurtures group solidarity. Trolls are sensational, yes, but we keep everyone honest. | Game Memorial

Nao

  • Dadman with a boy
  • Posts: 16,082
Re: Context object?
« Reply #2, on December 23rd, 2012, 04:06 PM »
Nope, remember... we::$var is 60% slower than $var, while we::get('var') with function get ($v) { return self::$$v; } or something like that would be something like 1000% slower... So it's definitely better to use method static variables than functions to retrieve a variable that's not within our scope.

Other than that, I'd like to know if any of these ideas made sense to you... ;)

Arantor

  • As powerful as possible, as complex as necessary.
  • Posts: 14,278
Re: Context object?
« Reply #3, on December 23rd, 2012, 04:13 PM »
The reason is that it has to do lookups to find the class and the way it does that is much the same as the way it looks up defined functions...

I have no problem with telling people not to be lazy. So if that means they have to learn not to use $context, that's fine with me.

I'm not opposed to such a class but it might be better served with other kinds of refactoring and making sure that what goes into $context actually needs to go into $context. Of course, when in loops it can always be pulled into a local variable, acted upon and if needed, later pushed back to $context.
Re: Context object?
« Reply #4, on December 31st, 2012, 07:42 PM »
Ahhhh.

Now I remember why we::$user or similar wouldn't work for me when I first tried it... it's PHP 5.3 only. We just effectively made Wedge PHP 5.3+ only.

When I previously tried it I was still using PHP 5.2 which is why it wouldn't work.

Nao

  • Dadman with a boy
  • Posts: 16,082
Re: Context object?
« Reply #5, on December 31st, 2012, 11:17 PM »
I probably forgot to declare the variables as static.
Can you verify it works if you do that? I'm pretty sure it does. 5.3 only allows NOT specifying the static keyword and still being able to access it.

Arantor

  • As powerful as possible, as complex as necessary.
  • Posts: 14,278
Re: Context object?
« Reply #6, on December 31st, 2012, 11:23 PM »
Given that it appears to be run through the LSB code which is 5.3+ only and it's even specifically referred to in the Zend PHP 5.3 certification as such...

Nao

  • Dadman with a boy
  • Posts: 16,082
Re: Context object?
« Reply #7, on December 31st, 2012, 11:42 PM »
I dont get it?

Im just saying, in we::$user, we need to declare public static $user instead of just public $user. Is that the case? I'm not in front of my computer for now...

Arantor

  • As powerful as possible, as complex as necessary.
  • Posts: 14,278
Re: Context object?
« Reply #8, on December 31st, 2012, 11:48 PM »
And I'm saying that as far as I'm aware, that won't work before PHP 5.3 for calling it outside the class.

I'm taking the Zend certification in two weeks, so I'm reading up, and in the study guide for PHP 5.3 it actually mentions this and says that it should only work in 5.3+, suggesting that it is reliant on the implementation of Late Static Binding (which would also explain the performance hit)

Mind you, I've found multiple other issues in the study guide so it wouldn't surprise me if it were actually wrong anyway. All I know for certain is, I tried to do something much the same a couple of years ago and it baulked at it because it was PHP 5.2. Whether something is slightly different or not, I don't know. Whether I did something different or not, I don't know. But I was following much the same basic approach then and it didn't work.

And I'm concerned that this effectively makes PHP 5.3 a minimum for Wedge. I'm not entirely convinced that's a bad thing, though.

I have to admit my temper is *a lot* shorter in recent times. I don't like Christmas. I don't particularly like New Year. I've spent a lot of time trying to make sense of all the changes to we::* to figure out why stylesheets aren't rendering properly any more (at all) and given up hoping that it'll just be fixed some time so that I can actually navigate the admin menus again... combined with the fact that I basically wrote all those little things last night to feel like I'm actually doing something useful rather than spending weeks farting around with something that just feels like a total waste of time.

Nao

  • Dadman with a boy
  • Posts: 16,082
Re: Context object?
« Reply #9, on January 1st, 2013, 12:54 AM »
I'll have a look when I'm on my pc.
Everything works fine for me locally, otherwise I wouldn't have committed. I'm very surprised.  Hopefully this won't represent the quality of my work in 2013 ;)

Arantor

  • As powerful as possible, as complex as necessary.
  • Posts: 14,278
Re: Context object?
« Reply #10, on January 1st, 2013, 01:03 AM »
Well, I found another bug that I've caused by my removal of loadUserSettings, which I've fixed locally, but ever since we was introduced, stylesheets are compiled but there's nothing browser specific in them, meaning menus don't have a background (which makes then practically unusable), the error log counter etc. is invisible, the background to the page is not a nice gradient but instead just blue, etc.

As far as my last point goes, yes, I could just ignore the whole package thing and just remove the add plugin setup, instead of faffing around understanding the zip format and making a nice class instead of the crap I found online (I think you'd rather I committed something around 80KB instead of a 199KB library, for example) and I'm just pissed off with how much work has gone into it with no visible benefits yet.

godboko71

  • Fence accomplished!
  • Hello
  • Posts: 361
Re: Context object?
« Reply #11, on January 1st, 2013, 02:00 AM »
Yet being the key word. You are doing great worK both of you. As for 5.3 hard to say its a bad thing really might annoye some.
Thank you,
Boko

Arantor

  • As powerful as possible, as complex as necessary.
  • Posts: 14,278
Re: Context object?
« Reply #12, on January 1st, 2013, 02:03 AM »
That's the thing, I'm not really the delayed gratification type. I like to see visible signs of progress as I go. The thought of spending two weeks staring at a piece of crap code[1] and without anything to show for it except a strong dislike of anyone else's code... yeah, it's as frustrating as it sounds.

After two weeks of attacking it, stripping it down to half size, I realised it could be done further but it would at that point be quicker just to start fresh, which I did.
 1. In his defence, he's written it as a C-style PHP script, not as a PHP destined one, so using p_ and v_ prefixes to indicate that things are pointers and local vars, not to mention dancing around returning 1 and 0 everywhere rather than actually returning values from functions. But it's still frustrating to have to clean up, especially the crazy ass indentation.

Nao

  • Dadman with a boy
  • Posts: 16,082
Re: Context object?
« Reply #13, on January 1st, 2013, 02:58 AM »
Pete -- I just can't seem to be able to install PHP 5.2 on my PC. It keeps sending me a connection error in WampServer, even though I followed the pretty straightforward install process. Anyway, problem is -- the system object DOES have 'static' keywords in front of our variables like $user. So it really should be working in PHP 5.2... I've even seen comments to that effect on the web.
Maybe (but it's very unlikely) you need to add '&' in front of the getInstance method in the object, to return a reference, but I don't see why that would change anything.
Anyway, I found this:
http://www.nqbao.com/2009/11/the-best-way-to-access-PHP-global-variable
The guy runs PHP 5.2.9 and look at solution #7... It's the same as mine, pretty much, only very simplified.
So I'm guessing it does work in PHP 5.2, it just doesn't work for you in that particular fashion.

Re: stylesheets, the main problem is that my own local version is uncommitted because it makes so many little changes here and there... I don't know exactly if committing them immediately would fix your problems.

Re: zip class, I don't know any better... I think a 80KB library is overkill already. But that's just me... If it makes life easier for everyone...

Arantor

  • As powerful as possible, as complex as necessary.
  • Posts: 14,278
Re: Context object?
« Reply #14, on January 1st, 2013, 03:13 AM »
I could have sworn that it wouldn't work. I don't know what I did differently that made it not work, and when I stumbled on the reference to it in the study guide, I figured that it was all related, and it probably is. I don't know. I just don't want to compromise Wedge working on PHP 5.2.

The problem with stylesheets isn't the content of the stylesheet itself as such, but the content of the WeCSS loader/handler barfing over we::something. I don't know anywhere nearly enough to even begin debugging.

Re: zip class. Well, the choices are simple.
1) We don't have add plugin or auto update at all.
2) I add a 199KB class that is beyond ugly.
3) I finish writing my own class that does the job. It'll be usable when finished. The thing is it's a complete Zip class, not just the minimum required for extracting files. If I thought that was acceptable, I'd do that, but I can see the benefits of a full zip class that I can redistribute separately as well.


Also: with what I'm working on, I'm going to be adding a copy of jQuery UI. Primarily for the sortable unit but it seems that for the admin panel we could just include and be done with it. I don't have the patience or mindset to sit and write my own sortable/draggable component right now, I'd rather just run with a proven and stable one for the time being but if anyone wants to write a newer, less bulky one, go nuts. Just don't break anything :P