Jump to content

[solved] what is $_html = '' for>


shoulders

Recommended Posts

Hi,

 

I am wrinting my own module and have been using the tutorial from www.ecartservice.net and there are 2 variables that seems to always be declared.

 

$_html = '' and $_postErrors = array()

 

now $_postErrors is used for errors, but is this a global 'thing' or just how the prestashop team have written it, ie the same in every module but could be called something else, or does this syntax have a particular job?

 

$_html = '' in the ecartservice.net tutorial is actually used to hold the html output from function getContent() and various other functions but in prestashop modules (at least the 1.5.3 version i am using) $output is used instead but $_html = '' is still decalred at the top. What is the purpose of $_html = '' ?

 

lastly and related, in the ecartservice.net tutorial _displayForm() is used whereas in the native prestashop modules displayForm() is used (without the underscore_, does the underscore have a particular purpose? what is just an old way of writing the variable for prestashop?

 

any help with this would be appreciated so i can write my first proper module and then update my postal module (see signature)

 

thanks

Edited by shoulders (see edit history)
Link to comment
Share on other sites

Hi there,

Actually, those variables are just conventionally named, but you can use what you want to hold errors and output code. i used $output for sometime, then moved to $_html since it's declared as an object property already.

I didn't check it for html and postErrors, but in PHP, you usually prepend an underscore to private or protected properties. I think that's all there is to it :)

 

As a personal opinion, I now use $_html since i don't have to bothwith returning $output in every additional function I use to handle getContent

  • Like 1
Link to comment
Share on other sites

thansk for the info Nemo1, somtimes you see several ways of doing things and i am never sure which one to go with. The question mainly came about because all the native prestashop modules i looked at used $output even though they declared the $_html

Link to comment
Share on other sites

You are right. Prestashop SHOULD have coding standards, but it doesn't. For example, that very same $_html variable should be assigned in the module class, but many modules still have it declared (like homefeatured) and as you spotted, use $output afterwards. This only makes things harder for use developer.

Edited by Nemo1 (see edit history)
Link to comment
Share on other sites

can i just ask one more question, it should be really simple for you but it is the last thing that is bothering me about the sample code i have gone through.

 

on ecartservice.net in his tutorial he uses a 'private' function for displayForm() whereas all the native prestashop modules seem to use a 'public' function for displayForm(). which is better private or public and why? I cannot understand why i should use a 'private function' ? if the answer involves a lot of reading i can do that, lol

Link to comment
Share on other sites

Yeah, it does involve a lot of reading. I suggest you try out some good PHP OOP book (there are plenty).

Public: this funcion can be use anywhere you want in the code

Protected. It can b used by this class and classes which extend this one

Private: oly this class can use it

 

They're used to make code more robust and secure basically (there's a lot more to it than a couple of lines i can write in a blog post of course ;))

 

Cheers!

Link to comment
Share on other sites

I understand all the basics of OOP, i have read some good tutorials and i can actually read and understand the code but i can not just get my head around why ecartservice in this particular code had decided to make the displayFrom() function private. because the code will work exactly the same public or private so why change it ?

Link to comment
Share on other sites

In OOP, public functions / methods should be used sparingly, only when needed, particularly in a library: once you made a function public, you are committed, if you want to avoid to break users' codes, to keep it, and to keep its contract (what it takes, what it does, what it produces) nearly forever.

That's a big burden, so only the functions really needing to be called from other classes are made public.

  • Like 1
Link to comment
Share on other sites

so to translate this for a noobie:

  • private functions should be used except where the programmer wants to share that functions output with the rest of the system, the data is controlled (basics of OOP)
  • it is a choice for the programmer on how neat to keep the system/code
  • public functions can be used everywhere if you wanted but this can be messy for large projects and data is harder to control, not so much of an issue for a home programmer with his first module or project
  • the underscode (_) denotes a private function and does not have to be used for things to work but it is convention and is an easy way to know if a function is private

Link to comment
Share on other sites

Yes, shoulders, I would say you got it right.

On point 3: yes, but beware, as you make a team with your future self: that's why even if you are alone, it is a good idea to keep code neat and documented, if you plan to review / update your code at a later date. :)

Now, getting code to run might be a higher priority.... :lol:

  • Like 1
Link to comment
Share on other sites

@PhiLho

 

on point 3, just to confirm, you would use the underscorce to denote a private function? I cant seem to work out if this contraveans the prestashop coding standards as set out in the developers guide here

 

the reason i am chasing all these little points is i have taken the skeleton.php from ecartservices to make a new skeleton 'module' for 1.5+ with heavy annotations to help me and other programmers just starting out.

 

I have discovered that there is always more than one way to do something in programming.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...