Jump to content

alexweber15

Members
  • Posts

    238
  • Joined

  • Last visited

Posts posted by alexweber15

  1. as in bad practice?

     

    I'm reading "Pro PHP Patterns Frameworks Testing and More" from Apress and the author constantly does that throughout the examples and I've seen it a bit around the forums in some examples...

     

    Wasn't it considered bad practice to do that???

    Because of superglobals and such?  I realize if you can restrain yourself from naming a property "GET, POST or SERVER" (and so on) then you probably wont have a problem but I'm just surprised at the popularity that using underscores to prefix property names has got :)

  2. guess these words are too generic for google :(

     

    basically i wanna know if in a class's methods i can pass the object as a parameter to another function:

     

    class Foo{
        protected $attr;
    
        function __construct($val){
            $this->attr = $val;
        }
    
        function sendMe($bar){
            if($bar instanceof Bar){
                $bar->getFoo(self);
            }
        }
    }
    
    class Bar{
        function getFoo($foo){
            if($foo instanceof Foo){
                echo $foo->attr;
            }else{
                echo 'not foo!';
            }
        }
    }
    
    $a = new Foo(4);
    $b = new Bar();
    
    $a->sendMe($b);
    

     

    this outputs: not foo!

     

    i've tried different methods like:

     

    $a = new Foo(4);
    $b = new Bar();
    
    $b->getFoo($a);
    

     

    this outputs: Fatal error: Cannot access protected property Foo::$attr

     

    i have no real practical use for this just for learning really :)

     

    is it bad practice for an object to have to pass itself as a parameter to another object?  (probably i guess)

     

    but even so, when i pass the object explicitly in the second example i still can't access its protected/private properties... (is there any way around this?  short of creating public methods to access the properties?)

     

    thanks!

     

    -Alex

  3. if you expect a parameter to be of a certain type is there any benefit in using type hinting vs instanceof?

     

    class foo{
    
        #ex1
        function parseValues(ObjectType $a){
            #call ObjectType method on $a
        }
    
        #ex2
        function parseValues($a){
            if($a instanceof ObjectType){
                #call ObjectType method on $a
            }else{
                return false; #or any other kind of error message
            }
        }
    }
    

     

    the way i see it whereas type hinting might be useful for documentation purposes as it makes it more obvious what kind of type you expect the passed parameter to be, the fact that it generates a fatal error = less flexibility.  in ex2 you have the same functionality but by using isntanceof you get the same security that the method you want to call will be there and it also allows you to return an error message of some sort without generating a fatal error.

     

    so in a nutshell:

    is there any benefit of using Type Hinting as opposed to instanceof?

     

    (afaik Type Hinting also implies that the comparison will be made implicitly; ie the 'performance hit' of using the conditional statement is the same in both cases)

  4. thanks thats a start! :)

     

    but they would have slightly different implementations afaik, like the FromDB would be required to implement standard CRUD functionality... and have some persistence logic somewhere...

     

    the FromFile would mainly be "CR"... (again a guess), but the XML and JSON data would probably only be required to feed other services such as SOAP or a RIA view layer, so really I'd just need to retrieve a select subset of the data and not necessarily have to manipulate it much like a database, assuming it would just be a cached file or something generated on request or something... dunno  ???

     

    :P

  5. looking to create a solid "model" layer for an application....

     

    - initially only mysql is used

    - we have plans to use xml and json in the very near future

    - since we're here, might as well make room for any other DBs... :)

     

    Im thinking of going with one big "StorageFactory" with 2 sub-factories being "DBStorage" and "FileStorage"...

     

    - Implementing each of the 2 "sub-factories" on their own is kind of straightforwards from what I've been reading and testing....

     

    Is trying to develop a "one-to-end-all" "StorageFactory" as described above a bad idea?

     

    Also, one thing thats been killing me is storing data is fine, but what about retrieving it?  maybe its a by-product of a well-implemented Factory for storing the data, but it just gives me the chills... someone said to use a REGISTRY pattern but is it really necessary?

     

    thanks! :)

  6. as far as i can tell, the following mods in bold to your script should make it work:

    also, the stuff i made an assumption: $_REQUEST["sender"] = the email address of the person sending the email

     

    in red = your email address, which as stated by their instructions must be hosted with them.... so check your MX servers or if you dont know how to do that contact them...

     

    ALSO, you get the order of params in the mail() function mixed up! :)http://www.php.net/function.mail

     

    the first param should be $sender (assuming my above assumption is correct) and also, you forgot to specify the additional "-f..." parameter they requested

     

    
    <?php
    
    // read the variables form the string, (this is not needed with some servers).
    $subject = $_REQUEST["subject"];
    $message = $_REQUEST["message"];
    $sender = $_REQUEST["sender"];
    
    
    // include sender IP in the message.
    $full_message = $_SERVER['REMOTE_ADDR'] . "\n\n" . $message;
    $message= $full_message;
    
    // remove the backslashes that normally appears when entering " or '
    $message = stripslashes($message);
    $subject = stripslashes($subject);
    $sender = stripslashes($sender);
    
    // add a prefix in the subject line so that you know the email was sent by online form
    $subject = "Contact form ". $subject;
    
    // send the email, make sure you replace email@yourserver.com with your email address
    [b]ini_set("sendmail_from", " [color=red]email@yourserver.com[/color]");[\b]
    [b]if(isset($message, $subject, $sender)){[/b] /* just a hint, you can pass multiple variables to a single isset() function call [url]http://www.php.net/isset[/url] */
       [b]mail($sender, $subject, $message, "From: [color=red]email@yourserver.com[/color], '-f'[color=red]email@yourserver.com[/color]");[/b]
    }
    ?>
    

     

     

    try my code, (and string the and tags!!) and if it still doesnt work contact tech support)

  7. sorry dont mean to autobump (at least not 5 mins after posting) but i couldnt find the EDIT button...

     

     

    - for the mysql directives i meant persistance is ON for local | and OFF for remote

     

    Also, on a side-note, do these mysql extension options affect the newer mysqli?  afaik mysqli has no explicit persistance directive...

    thanks! :)

  8. taking a look at my local phpinfo() and remote phpinfo() configs i see some differences that im not sure i should be worried about (both are v5.2.6 btw):

    setting:localremote

    Server API:Apache 2.0 HandlerCGI

    Thread Safety :enableddiabled

     

    mysql (plan on using persistent connections)

    mysql.allow_persistent:localremote

     

    mysqli (what i actually use for all db stuff)

    Client API library version:5.0.51b 5.0.45

    Client API header version:5.0.51a 5.0.22

     

    CGI INFO btw (in case its useful):

    DirectiveLocal ValueMaster Value

    cgi.check_shebang_line:11

    cgi.fix_pathinfo:11

    cgi.nph:00

    cgi.rfc2616_headers:00

     

     

    - my main worry is running PHP as CGI (never done it before)

    - also what are the implications of disabled thread safety and the slightly not as up to date as my local apache's mysqli directives?

     

    i use a few API features basides the "bread and butter" (connect, close, select_db, query, error, errno, insertID, affected_rows), namely: commit, rollback, use/store_result, and 1 or more i cant think of off the top of my head...

     

    should i hassle them and get them to update?  (all shared servers, so gonna be a pain in the ass and take forever) or are these differences negligeable?

     

    thanks!!! :)

  9. good to know :)

     

    an observer is a bit (lot) more complicated... :)

     

     

     

    lol. Just starting to investigate it.

    It sounds like you've got experience with it.  Any hints and tips?

     

     

    to say ive got experience with it is a complete overstatement :)

    ive been studying design patterns intensely for the past few weeks and have received a lot of help from ppl here on the boards (and many others) and from professors at uni so ive got at least concepts and theoretical implementations down, kind of :)

     

    im at work right now so i gotta be kinda brief, but check these links out, they are really helpful! :)

     

    http://en.wikipedia.org/wiki/Observer_pattern

    http://www.fluffycat.com/PHP-Design-Patterns/Observer/

    http://devzone.zend.com/article/5-PHP-Patterns-The-Observer-Pattern

     

    also there's a ton of published literature on the subject, my favorites being:

    - "The Gang of Four: Design Patterns, Elements of Reusable OO Software"

    - "Architect's guide to php design patterns"

     

    ill leave the details and ethics of how to obtain them up to you :)

  10. Hey, just to make sure we're talking about the same things here... when you say Controller.php do you mean Lemon's example or yours?

     

    Also, as a conceptual/generic side-note:

    Singletons should have a private constructor (or in your case maybe protected since you want to extend from it) and all the interaction to $instances should be done via a getInstance() function... but really, extending a Singleton is kind of um, contradictory in a way... :)

  11. afaik instead of using all the references (which also afaik is redundant since objects are passed by reference anyways).... although this is a generic example and doesn't really apply to the specifics of your project, here's a good example of a singleton implementation:

     

    class Singleton {
    
    private static $instance = null;
    
    private function __construct(){}
    
        public static function getInstancia() {
        	if (self::$instance == null) {
        		self::$instance = new self();
        	}
        	return self::$instance;
        }
    }
    

     

    hope this helps :)

  12. thanks! :)

    i talked to a Prof at uni today and he showed me a "simple" PHP/MySQL CMS all the way from the UML Case of Use, Activity Sequeces and Class diagrams and the parts about Persistence and DAO were really helpful... very reassuring that you guys agree :)

     

     

    what about the Validator though???

     

    is having a static validator a bad idea?  (i forget whether static classes can have private attributes but i doubt it)

     

    if not, where in the whole process would i implement validation? (in the HELPER?)

     

    thx!!!

  13. In a nutshell this is the interaction sequence: (doubts are in red)

     

    • User posts html form
    • Form is validated

     

    If validation passes:

    • Clean Up data ?
    • Instantiate Class representing a filled form (ex: User Profile)
    • Newly UserProfile object sticks around for duration of session AND passes itself to "StoragePrep" Class, which in turn prepares the data for storage (XML, SQL, etc) and finally passes itself to the corresponding class that will perform the CRUD operations

     

     

    - is data cleanup really necessary at this point?  (assuming strict validation?)

    - is a class passing itself as an argument to the next class' constructor (ie UserProfile to StoragePrep) a good practice considering it might still be active if the user does something else (reference issues here)

    - can any of these afforementioned classes be static?  (for example StoragePrep and Validator specifically)

     

    BTW: The project has no inheritance and relies on Type Hinting using interfaces to make sure the correct parameters get passed back and forth

     

    thanks! :)

     

    im trying to makes this as loosely coupled as possible and the interface over inheritance really helps, but i think i might be instantiating too many unecessary classes....

     

    any feedback, thoughts, tips, criticism, cookies or beer appreciated! thanks! :)

     

    -Alex

  14. It wasn't me who helped you the most. Thank guys above ;)

     

    You're welcome of course :)

     

    True that, and of-course, A HUGE COLLECTIVE THANKS TO ALL WHO HELPED! :)

     

    I've gone on a posting rampage the past few days and I've seen a lot of familiar faces helping me so I hope I can give back soon when I'm more proficient! :)

     

    on an additional note, I created a new post on Design Patterns which should hopefully pick up where this thread left off! :)

  15. Here's some brief context:

     

    1) User fills out one of 2 forms (both share 10 common fields, each form has an additional 5 specific fields - so both have 15 fields total)

    2) Form gets posted, validated, etc, etc

    3) Objects are created according to a valid form submission for manipulation by other parts of the system and the database layer...

     

    Each form (FinanceLead and InsuranceLead) corresponds to a different variant of a similar object (a Qualified Lead) and given their similarity and "is-a" relationship, I initially wanted to model this using Inheritance, with both FinanceLead and InsuranceLead inheriting from a Lead class (which would include all the common elements, leaving the specifics of each type to its own class).

     

    However, I've been advised that this strong coupling could lead to problems in the future and with the help of posters on this board I've we've up with an alternative class model based on an interface and no inheritance, which should solve the strong coupling issue.

     

    Ok, now for the interesting part:

    With the initial inheritance-based model, I opted to use a SimpleFactory design pattern.  In short:

    1) SimpleLeadFactory: creates leads on demand, based on class constants that determine the LeadStrategy (Insurance or Finance), so basically the final SimpleLeadFactory would use a static methods to create and retrieve the leads on demand.

     

    but...

     

    2) Without inheritance, this no longer seems necessary... so i'm going through a design pattern crisis here... I don't want to use one just for the sake of using one, but really, can my code scale well and be extensible and future-proof without adopting some proven design pattern or methodology????

     

    Can anyone please comment, suggest, anything to help me out please?

     

    Here's what I have so far (includes and such left out for readability):

    interface LeadStrategy{
    // list of required functions
    public function doSomething();
    }
    
    class LeadSeguro implements LeadStrategy{
    // specific insurance attributes
    private $attribute;
    
    ### Constructor and Destructor ###
    public function __construct(){
    	// do something
    }
    
    public function __destruct(){
    	// do something
    }
    
    ### Implementation of Interface Functions ###
    
    public function doSomething(){
    	echo 'Lead Seguro doing something';
    }
    }
    
    class LeadFinanciamento implements LeadStrategy{
    // specific financiamento attributes
    private $attribute;
    
    ### Constructor and Destructor ###
    public function __construct(){
    	// do something
    }
    
    public function __destruct(){
    	// do something
    }
    
    ### Implementation of Interface Functions ###
    
    public function doSomething(){
    	echo 'Lead Financiamento doing something';
    }
    }
    
    class Lead{
    // specific Lead object
    private $theLead;
    // generic attributes
    private $attribute;
    
    ### Constructor and Destructor ###
    
    public function __construct(LeadStrategy $type){
    	// todo: check if data matches hinted type
    	$this->theLead = $type;
    }
    
    public function __destruct(){
    	// nothing yet
    }
    
    ### Implementation of Interface Functions ###
    
    public function doSomething(){
    	$this->theLead->doSomething();
    }
    }
    

     

    Thanks and sorry for the super long post! :)

  16. hey, since your out and about on the forums atm i'd like to bother you with one more question if you don't mind :)

     

    as far as i understand:

    even though you didn't define constructor methods for the InsuranceLead and FinanceLead classes they should in fact have constructors to initialize their private attributes right?

     

    Reason I ask is I was considering using a SimpleFactory design pattern (based on my previous inheritance-based Lead class architecture), but now I'm having second thoughts as to whether it's really necessary, since the Lead class you proposed is at first sight robust enough to carry out any of the tasks the SimpleFactory object would have had to do, in terms of generating leads of a specific strategy... right?

     

    if i'm being vague i'd be happy to explain further but I think you'll get the gist of it :)

     

    thanks again! :)

  17. hmmmm gotcha! :)

     

    so then i would always instantiate Lead objects, which would be defined more specifically by their strategy attribute.

     

    i like :)

     

    also, on a side note, i didn't realize you could use an interface as a function parameter (not sure i used the correct terminology here but i think you know what i mean right?)

×
×
  • 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.