Jump to content

RuleBritannia

Members
  • Posts

    92
  • Joined

  • Last visited

Everything posted by RuleBritannia

  1. I think I had a brain messup when designing this weeks ago. I guess its "properly" done by passing the values in a method that's callable manually other that __construct. This way we don't have to destroy and start new objects. Sorry for the question, Unless I have still got it wrong, in which case the question still applies. Thanks
  2. Hello If we pass a value to a object when creating it with __construct $NewObject = new class($value) Lets say our object is placed within a loop to do some further stuff while(1 < 10) { $NewObject->doThis(); $NewObject->doThat(); 1++; } Now, we want to pass the next value to the object, so it will doThis() and doThat in the loop, But currently, it will just perform doThis() and doThat() on the same value. How is this professionally achieved? We can destroy the object and put $NewObject = new class($value); inside the loop, But is there a better way? Thanks in advance for any future help/thoughts.
  3. They all perform the same overall task(logging in to another website(A,B,C,D)), but each one will take shorter or longer to do. A B C D will have different requirements within that must be done in order to complete the task.(crsf tokens, redirects, etc)
  4. Here is a example which should be easy to get the general idea. class A { var $var = 'A'; function get_letter() { return $this->var; } } class B { var $var = 'B'; function get_letter() { return $this->var; } } class C { var $var = 'C'; function get_letter() { return $this->var; } } class D { var $var = 'D'; function get_letter() { return $this->var; } } class letter { var $many; var $letterClass; __construct($value) { $this->letterClass = $value; $this->many = new $letterClass(); } function get_letter() { return $this->many->get_letter(); } } $new = new letter('A'); echo $new->get_letter(); We can handle DB inside the classes A,B,C,D, var $var = 'A'; function get_letter() { $handle = array( 'id' => '1', 'start' => '2010/02/02 04:04:04' ); update($dbobj,$handle,$table); echo $this->var; $handle = array( 'id' => '1', 'end' => '2010/02/02 04:04:05' ); update($dbobj,$handle,$table); } } Also applying same code to B,C,D(duplicate code) or We can handle outside via the main class class letter() function get_letter() { $handle = array( 'id' => '1', 'start' => '2010/02/02 04:04:04' ); update($dbobj,$handle,$table); echo $this->many->get_letter();; $handle = array( 'id' => '1', 'end' => '2010/02/02 04:04:05' ); update($dbobj,$handle,$table); } } This way "seems" best, as no code duplication, But I am wondering if this is a correct pattern to follow. From a pattern/architecture point of view, once things need to be expanded etc, problems may arise?(That I am unaware of at this state in design due to limitations of foresight specifically in OOP) Thanks in advance.
  5. I could inject into the many, But this would be alot of code duplication, each of these "many classes" have around 8 methods, so I would have to inject into 8 methods within many classes. As for timestamps, I dont think it would be a huge difference, But handling the timestamp and updating directly after completition would be more accurate than updating outside of the class as there is room for other things to happen in between
  6. Hello. I have many classes all containing identical method names(different coding in each method), Which I control via 1 main class which can call any of them. I am wondering the best place to handle DB interaction. It seems best to handle once in the main class, but one problem can be datetime sql statements not as accurate, Along with possible other issues. If we handle within each of the "many" classes I described earlier, we will get exact timestamps, but then we will have alot of code duplication. I assumed some of you have come across this issue before? In all the books I have read I have not come across a "best" pattern for this. Above all this I am trying to stick with universal class principles(Creating classes which can be used elsewhere independently). Perhaps I am trying to run before I can cry? Thanks for any answers/views in advance.
  7. That is exactly what he said. "If you are going to asign the value to a variable then you must want to use it further on in the code. So assign first, then echo out the var." I wouldn't do what you said, as I need the variable.
  8. Yes your right, I didnt seem to notice it in this context. With that being said, I think I will use it universally when I can. Thanks
  9. by assigning first then echoing like you suggest, it will look like this $var = 'this var was undeclared before this'; echo $var; echo $var = 'this var was undeclared before this'; seems to do the same, so whats the point of taking another line of code?
  10. I checked numerous reputable sources(php.net/stack overflow etc) couldn't find any answer in regards. echo $var = 'this var was undeclared before this'; As you can see, I am echoing and setting a variable all at once. It seems to work ok with no errors, but I do not remember reading this in the past in any php books, nor can I remember seeing it in any open source code. Wondering if it is bad practice? On another note, echo $var[1] = 'this array and key|value was undeclared before this'; works too Thanks in advance.
  11. Ok noted, Also I never rip out work for what you mentioned. (Altough that probably explains why it takes me months to get anything "done")
  12. @trq, Ok reading this now, Seems quite in depth from my first glance at some github files, Only started OOP about 2 days ago. @ignace, I give a "canned" example to allow for a more precise comprehension for the reader, hoping that if they are more competent they will see it and say "no, what your trying to achieve is not best done like this, Its better approached like blabla...." even though the "canned" example approach may work. If the "canned" example I gave turns out to follow a good/best protocol, then I guess it cant be improved, But I have only started OOP 2 days ago, And in reading around 10 OOP tutorials, This wasn't covered. Before I go implementing bad techniques, I would rather learn the best way from the outset.
  13. Hello If I have 5 methods, But 4 of them can only run once one has been run and returned true, Whats the best OOP approach for this. I have this working solution, But I dont know if its what the professionals would do. class oop { var $status; function main() { $this->status = true; } function subone() { if($this->status =='true') { other stuff here etc } } function subtwo() { if($this->status =='true') { other stuff here etc } } } I have checked alot of oop sources, but i don't see a set out way to tackle conditional oop methods Thanks in advance.
  14. OOP is more then simply introducing one class into your project. Or a few classes for that matter. So if you want to use a class for it or go with a function, the choice is ultimately yours. You can avoid having to create the handle over and over by passing the handle to the functions that require them: Yes, But this situation seemed to be best approached in a OOP, In regards to passing the handle, I have around 4 different logins(for 4 different sites), So im not sure if passing the same handle to each of them is a good idea, I thought putting each handle inside a construct before each login process. Personally I would prefer a class to signify the cohesion between the functions and not having to pass the same data around between functions, in a class they would simply share the information. But don't take my word for it though, because I am biased. Yes the sharing is where I began to see the better view of OOP. Thanks for your view!
  15. I'm curios to know if the OOP approach would suit this problem over procedural.(from your perspective) I am using curl, to fetch and post information. I believe its best broken down into stages login, fetch, post. Before we can fetch or post, login must be done. To improve efficiency, once login is done once and confirmed, if we are fetching or posting any other times(loops, later etc), we don't have to keep logging in(use of constructor applies here) For curl efficiency, Also put curl_init inside constructor? and put curl close inside destructor? Another note here, Im not sure if reusing one handle is buggy?, But it seems logical to do it this way(however google shows otherwise) In the past I have achieved the same final result(without being efficient, also requires more code lines.) wasteful example upload function make handle (ch) login. (perform post) upload (perform post) close handle fetch function make handle (ch) login. (perform post) upload (perform post) close handle whenever its needed, just bang it in a loop and it will work. Sorry if this question seems overdumb, But Ive never done anything too complex to warrant OOP, So it never seemed logical for me to switch over. Maybe even what im trying here doesn't need OOP? But im hitting problems with procedural... Would be interesting to see your views, also any improvements. Thanks in advance.
  16. If there was no function to match the brand, When calling $brand(); it wouldnt work. This approach looks better, thanks I would not do this in a program which is created for people other than myself, This application is just for me.
  17. As for how the data got in the table, I set it myself, So there wont be any brands without a function etc, Either way, This example probably falls into your "illegitimate" uses for this methodology. If you care to provide a example, How would you personally deal with this? Thanks in advance.
  18. Thanks. In regards to what im planning, more like already planned and executed. I have some scripts which pull data from a table, this table data like name > core i7 brand > intel for each brand I have made functions, which have the settings for the brand pages(diff bg , header sizes, etc) When I query the table, I can call the function like $query['brand'](); So whatever is that value, the corresponding function will be called.
  19. Thanks for your in depth response, But I wanted to ask 1-2 questions. "No support from the IDE about what is executing." I dont use a IDE, Just notepad. "No help from your IDE about possible errors" Again. "Slow function calls." So this actually causes more resources? A big amount or? "Prone to bugs that can be difficult to track down." If you captured error messages at all, Wouldnt the same apply however you called the function? "Adds another attack vector to your code." Yeh I can see this is a good point. Overlooked this. "Using a feature just for the sake of using a feature." It was logical in this case from my view at the time, so wasnt just out of the sake. "Lends itself to magical behaviors that are crazy to maintain and support." What other magical behaviors could those be? "Unreadable." Yes I guess, but only planned to use it once.
  20. So, Throughout my script it seemed logically correct to call a function via a variable value. Example I have function computer() { return 'pcs are good'; } function laptop() { return 'laptops are ok'; } $value = 'computer'; echo $value(); $value = 'laptop'; echo $value(); However, After just googling this, I saw alot of criticism towards this technique, Whats the problem with this? Thanks in advance.
  21. Came up with this, still not elegant enough for me to stop thinking thought $array1 = array( 'width' => 400, 'height' => 300, 'start' => 200, 'end' => 10 ); $array2 = array( 'width' => 4000, 'height' => 30, 'start' => 20, 'end' => 100 ); function c($a,$b) { if($a <= $b) { return 0; } elseif($a > $b) { return 1; } } print_r(array_uintersect_assoc($array1,$array2,'c'));
  22. That wont work as array_diff_assoc checks if each value is equal(===) to one another. So if we set width in array2 to 40000, It will be returned as a lower amount, When it isn't. Thanks anyway though.
  23. Array 0 ( [width] => 400 [height] => 300 [start] => 200 [end] => 100 ) Array 1 ( [width] => 40 [height] => 30 [start] => 20 [end] => 10 ) Say I have these 2 arrays, I want to check if array 0 values are all less than array 1. Is there a function to do this? Or possibly use a function specified for something else to achieve what we want? Thanks in advance.
  24. Excellent answer, This is exactly why I post questions like these. Thanks alot!
  25. Hello Yes I am aware a foreach can do it, but just wondering if there are any other alternatives(built in functions etc)
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.