KevinM1
Moderators-
Posts
5,222 -
Joined
-
Last visited
-
Days Won
26
Everything posted by KevinM1
-
Pretty much. Remember that in PHP 5+, objects are passed by reference by default, so if you instantiate a $database object upon request, and then pass it to other objects, those secondary objects will all be using that one lone $database object. That's one of (the many) reasons why doing the opposite - a Singleton-like super object that contains other objects - is pointless. You don't need Godzilla in order for things to talk with one another. If you've looked at some of the DI stuff me and ignace linked to, you might be thinking that a DI container is a super object. It's not. It's merely a mapper that says, "An object of class X depends on an object of class Y... okay, I'll link them when a new X is made." It takes a little while to get used to, especially if you're trying to roll your own. That's one of the reasons why I like to use a solid framework - DI is usually baked right in, and if not, it's usually not hard to find a separate component that can plug right in. Ultimately, it's all about setting up object requirements before you instantiate, and having the object work more or less out of the box when you call new X;
-
1+i Man, my math is rusty. Is that complex or imaginary?
-
Hehe, fabpot. Yeah, everything I've seen from him/Sensiolabs looks really good. Even Twig, and I'm not a fan of template engines. I need to find a project to actually use Symfony on.
-
The biggest problem, from a design aspect, is that you're trying to mesh different layers of your app into one. The db is deep in the back end for data persistence, while routing, sessions, and smarty (especially) are a part of the presentation side. Simply put, they shouldn't be contained by a general purpose super object. And, really, the idea of a super object is essentially antithetical to OOP in general anyway. Creating your app environment (the classes you'll need) is the job of a bootstrap, which should only be concerned with setting up/instantiating these kinds of things and not their eventual interactions. From there, you'll need to separate your code into tiers. One of the unfortunate things about PHP frameworks is either that they suck, or don't really illustrate how MVC is supposed to work beyond canned examples. MVC is a presentational pattern. In non-trivial apps, the M really refers to what is known as view models, which are simple DTOs (Data Transfer Objects) that contain POD (Plain Old Data) and whatever getters and setters are necessary to access it. The DTOs do what their name infers - they transfer data from the back end to the user, and then back again. The real brains of the app lies below MVC, in the domain, which is where the application logic actually happens. MVC is really just a skin on top of a domain. It's supposed to handle the user interaction component, but not much else. More importantly, this means that MVC is really just the front facing layer of a non-trivial app. Below it is the domain, and below that the DAL (Data Access Layer: domain <-> database). The resistance and confusion you're experiencing is to be expected because, like I said earlier, you're trying to mash layers together. All that said, if classes rely on external dependencies in order to work, that's a clear sign of needing to implement Dependency Injection. Creating Singleton-esque classes is exactly the wrong way to go. Here's a great link by one of the creators of Symfony that explains what DI is, and how to use it:
-
First, you don't need to assign by reference (=&) if you're using PHP 5+. Second, this kind of thing, on the surface, looks like an exercise in coupling unrelated classes/objects together, which is a Bad Idea in OOP. What are you actually trying to do?
-
This topic has been moved to Application Design. http://forums.phpfreaks.com/index.php?topic=363561.0
-
As a slight digression, never, ever, ever use 'global'. For anything. In any context, but especially in an OOP context. Ever. The 'global' keyword creates incredibly brittle code, tying/coupling what should be modular and reusable code to the environment in which it is invoked. What you should be doing is storing an instance of your $database as a protected member of your abstract base class. It should be passed in via its constructor. This is known as Dependency Injection*, and is the way to go. Even in a procedural context, 'global' is bad. It creates an implicit requirement for a function to work properly by completely circumventing its signature. Anything a function needs from an exterior scope in order for it to work should be passed into it via its argument list. That's why it's there. There is nothing to be gained by using 'global' except for inevitable headaches down the road. Any resource using 'global' should be considered suspect. And, yes, that includes apps like WordPress and Joomla, and whatever frameworks that are out there that do it. *Usually DI is a bit more complicated than that, but the basic idea is that if a class depends on an exterior object in order to do its job, it should contain a reference to that exterior object. The most common way to grab a hold of that reference is to pass it into a constructor. That said, it's certainly acceptable to simply pass it into whatever method(s) that need to use it.
-
What do you consider 'complex'?
-
mysql_result() expects parameter 1 to be resource
KevinM1 replied to whopunk123's topic in PHP Coding Help
The 1st reply states that he has his die(); in the wrong place, which isn't the problem at all I'm not trying to rage at anyone, or anything like that, I'm just asking why everyone is talking about the die(); function when that's not the problem xD The die() is written at the end of an assignment statement, not a function invocation. It will always die at that spot. Wow, did I screw this one up. I didn't read through the OP's code with the attention/diligence it required, and, as a result, gave a really horrible and incorrect answer. So, to the community at large, and to my fellow staff, I apologize. I hold myself to a higher standard that was displayed here, and this will serve as a good reminder to not slack off. I also believe that since I take other members' lazy/incorrect answers to task publicly, it's only fair that I own up to my own lazy and incorrect answer publicly. So, once again, I apologize. --- That said, I think it would be good if we found out from whopunk123 exactly what they want to do with the data. I mean, they're trying to get up to 30 rows of the 'balance' column. Is that data supposed to be added together? Output one row at a time? Getting the full picture will allow us to give a better answer. -
mysql_result() expects parameter 1 to be resource
KevinM1 replied to whopunk123's topic in PHP Coding Help
The 1st reply states that he has his die(); in the wrong place, which isn't the problem at all I'm not trying to rage at anyone, or anything like that, I'm just asking why everyone is talking about the die(); function when that's not the problem xD The die() is written at the end of an assignment statement, not a function invocation. It will always die at that spot. -
In PHP, arrays are not a reference type. To have the arrays modified by reference, you'll need to specify it manually: $i_Changed = stream_select (&$r, &$w, &$e, null);
-
The 'var' keyword is deprecated and shouldn't be used. Any tutorials that have it are old (PHP 4) and likely not very good. OOP is really something that isn't well suited to tutorials anyway. The syntax - how to write classes, invoke methods, etc. - is really the least important part of it. Slapping a bunch of thematically similar functions in a class is NOT OOP. So, get good books. Matt Zandstra has the best OOP for PHP book around. After you go through that get the Gang of Four's book. OOP is the kind of subject that's worth getting good resources for.
-
Really ? $a = 'Z'; for ($i=0; $i<26; $i++) echo $a--; $a = 26; for ($i=0; $i<26; $i++) echo $a--; Hehe. Yeah, you've proven us wrong. To be fair, I never had to increment characters in PHP, so when Barand said it worked, I figured that was because PHP is written in C and simply borrowed that functionality. Learn something new every day.
-
Not true, PHP has loose typing. In fact you can use arithmetic operator on non-numeric characters eg $a = 'A'; for ($i=0; $i<26; $i++) echo $a++; //-> ABCDEFGHIJKLMNOPQRSTUVWXYZ I believe that works because PHP uses ASCII characters, which have a contiguous range with the alphabet character codes. Basically, since each character has an underlying numerical code, it's treated as an integer under the hood, so arithmetic operators will work on them. It's less to do with loose typing and more to do with being built on C (which is statically typed, but handles character arithmetic the same way).
-
Semicolons are irregular as separators. The ampersand is by far the most used method. Ahh okay, sorry for my misleading information. I've learned something here too Thanks, pseud More info: http://en.wikipedia.org/wiki/Query_string
-
Semicolons are irregular as separators. The ampersand is by far the most used method.
-
Of course you can: http://www.example.com/index.php?id=1&somethingElse=45&anotherThing=bubba
-
Regarding Dan's point about being able to use a subset of data as a representative of a larger set, that's what Test Driven Design with mockups are for. You never need real data for developing functionality. All you should care about is whether or not the data set CRUDs (I just invented a verb!) properly or not. You can do that with: Name Address SSN CC# Mickey Mouse 35 Disney Lane 000-00-0000 1234567890123456
-
In the future, when posting code on the forums please place that code within tags. I took the liberty of doing it for you above.
-
This topic has been moved to Application Design. http://forums.phpfreaks.com/index.php?topic=363282.0
-
this may be harsh, wtf is the subject of the post? ""? The subject of the post is trying to figure out what the hell the purpose of your original post is supposed to be. If you haven't noticed, this is the PHP CODING Help subforum. Which means that this is the place for people to ask for help with their (wait for it...) code. It is not the place to have discussions about coding. That would be either the App Design subforum or the Miscellaneous subforum, depending on how abstract the conversation is. Why? What makes you say that? Why not Tetris? Why not Solitaire? Why not Othello? Yay, spam. Thread locked.
-
What's the relationship between Java and JavaScript?
-
Yeah, that's Zandstra's book.
-
That's because online tutorials are generally shitty and hardly ever show why OOP can be efficient and flexible. Instead, they begin and end with syntax and canned examples like a Cat class deriving from an Animal class. Get Matt Zandstra's book, then the Gang of Four's book. They will illustrate real world examples (especially the GoF book). Suffice it to say that there's a reason why OOP skills are generally required by professional grade dev shops.
-
Em is 'better' for fluid and responsive sites because its size is dependent on the browser's font size (an em is equal to the size of a lower case 'M'). Usually, 16px = 1em. There are sites out there that allow one to do pt to px to em conversions.