Jump to content

KevinM1

Moderators
  • Posts

    5,222
  • Joined

  • Last visited

  • Days Won

    26

Posts posted by KevinM1

  1. @TheNatalShark, please post your code in code tags, like ignace did in his snippet. You can do it either by clicking the <> button on our editor, or by manually placing your code within

    
    

    tags. I've taken the liberty of doing it for you in your previous post.

  2. 1. A class defines a type, which is both data and the series of operations that can be performed on that data. Example: integers. Integers are whole numbers that can be modified via arithmetic. Because you're defining a type, it really doesn't make sense to put a class definition within an if-conditional. I mean, you wouldn't hide the ability to create an integer behind some conditional, would you? Or a function definition?

     

    2. Class properties should be labeled either protected (if you're going to subclass) or private. Why? It keeps you or someone else from accidentally overwriting them by ensuring that data manipulation happens only through clear lines of explicit communication.

     

    3. You really shouldn't output in class methods. Methods should return values unless you're writing a class whose purpose is to handle displaying data.

     

    So, with all that, I'd rewrite what you have as:

     

    class BankAccount
    {
        private $balance;
        private $name;
    
        public function __construct($balance, $name)
        {
            $this->balance = $balance;
            $this->name = $name;
        }
    
        public function getBalance()
        {
            return $this->balance;
        }
    
        public function setBalance($newBalance)
        {
            $this->balance = $newBalance;
        }
    
        public function getName()
        {
            return $this->name;
        }
    
        public function setName($newName)
        {
            $this->name = $newName;
        }
    
        public function deposit($amount)
        {
            $this->balance += $amount;
        }
    
        public function withdraw($amount)
        {
            if ($this->balance < $amount) {
                throw new Exception;
            } else {
                $this->balance -= $amount;
            }
        }
    }
    
    $name = $_GET['user'];
    
    if (isset($name)) {
        $account = new BankAccount(3500 /* or some other amount obtained from someplace (db?) */, $user);
    
        try {
            $account->withdraw(4000);
        } catch (Exception $e) {
            echo "You cannot withdraw more than what is in your account";
        } 
    }
  3. But, again, our purpose here is to help people with their coding questions. And since it's been six years since you originally asked your question, I think it's safe to say that no one here knows of a tool to do what you want, and if even they did, they're not sharing that info for whatever reason.

     

    Time to move on. You won't find what you're looking for here.

  4. Every object has a constructor.  Test's explicitly empty constructor is a bit redundant since if you try instantiating a class that doesn't have a constructor, PHP's general purpose default constructor will execute instead.  A working example of that in action:

     

    class Example
    {
        public function blah()
        {
          echo "blah";
        }
    }
        
    $x = new Example();
    $x->blah(); // echoes "blah"
    Notice that new Example still worked even though I didn't write a constructor.

     

    Foo's constructor is written so that it will only accept an object of type Test as an argument.

  5. Like everything else, IDEs come with their own pros and cons.  I personally like IDEs because they integrate and automate a lot of things that make my life easier, like debugging and using git.

     

    I think the only way to make an informed decision is to try some out and see if you like them.  Most are free, or come with a free trial.

  6. Okay, first, some tips:

    1. NEVER post sensitive info on a public forum like this one. I took the liberty of editing out the passwords in your post above, but, yeah, not smart.

    2. Why are you passing a user's name and password through sessions? All you need to know is the user, right? Each user should have their own account. Successful login should set a flag that says "I'm logged in as Bob," or "I'm logged in as Suzie." Passing their passwords around, especially in a non-hashed plaintext (meaning, exactly what they enter into the system) is both unnecessary and dangerous from a security standpoint. You might think, "Well, this is a small site... who would ever see it?" But, small sites are perfect targets for bad guys because they tend to have bad security. And since most people still use the same password for everything in their lives, a security hole on your site could eventually mean someone having their identity stolen.

    3. Why are you checking for a certain usr/pswd combo only to reassign it with the same exact data immediately afterward? The:
     

    $_SESSION['usr'] = "AINSCOUGH";
    $_SESSION['pswd'] = "*****";

    Portions are completely unnecessary.

     

    ---

    From what I can see, you've written yourself into a corner. Your current system is inflexible. Yeah, you could hack at it, with a bunch of if/else conditionals to make it 'work' (and you could), but that wouldn't really be addressing the underlying problem.

    I suggest that you rebuild it so it's flexible and secure. Look at my first reply: that tells you the general approach you should take. If you need any help on any particular part of it, we'll be here. And while that's likely not what you wanted to hear, it's really the best way to go for you and your employer. I simply can't, as a web professional, give you a way to proceed with your current code in good conscience.

  7. That's a pretty archaic way of handling users.  Ideally, you'd have a User table in a database that contains their login name and a hashed version (never, ever, EVER the plaintext version) of their password.  When they log into the system, you'd check the data they entered against the data that's in the database.  If it matches, you then set a flag (it could be something as simple as $_SESSION['loggedIn'] = true;) that you pass from page to page.

     

    If you need to limit what logged in users can see based on their access level, simply add that info to the database and pass that around as well (something like $_SESSION['accessLevel'] = 'admin';).  Then you can simply check if they're logged in and if they're at the proper access level to view the page.

  8. A slight tweak to Some_Class (I'll rename it SomeClass):

    class SomeClass
    {
        public $userFile;
     
        public function __construct($file)
        {
            $this->userFile = $file;
        }
    }
     
    $myFile = new SomeClass($_GET['userID'] . "-file.txt");

    I like it better that way because otherwise the class is dependent on $__GET['userID'] existing, which isn't good.

  9. That's because addslashes has never been a proper method of escaping database input.

     

    The best way to escape input is to use prepared statements with either MySQLi (notice the 'i' at the end... the old mysql_* functions are deprecated and should not be used), or PDO.  The online PHP manual has examples of both.

  10. I do believe that "new" is a reserved keyword in PHP, so you're not supposed to name anything that.

     

    More to the point, the proper way to instantiate an object is to write something along the lines of:

     

    $obj = new <class name>(/* argument list */);
    Where <class name> is what you choose when you write the class definition:

     

    class <class name>{    // class definition}
    If you want new DB to work you need to rename your class "DB".
  11. You think they're correct DTDs? Have you run any kind of HTML verification tool or, you know, looked at those sites in different browsers?

     

    IE9 should default to standards mode, and only go into quirks mode if the doctype is messed up. If you need to fix your doctypes, just use <!doctype HTML>. That's the HTML 5 doctype, but properly formatted 4.01 or XHTML will work just fine with it. Much easier to deal with than the crappy old doctype declarations.

  12. 1. No one is going to go through that much code to figure out your problem.  You need to show us just the relevant part.  We're not a place where you can just dump non-working or incorrectly-working code, say "fix it," and have it done.  That's not what we do.  We're willing to help, but only if you (or anyone) has shown they've attempted to work on it your(them)self(ves).  If you need more than that, you should consider hiring a developer.

     

    2. Please place any code you want to post within code tags.  You can do it by pressing the <> button on the text editor here, or by manually placing your code within

    ...

    tags.  I've taken the liberty of fixing your previous posts for you.

  13. Yes, it was. And I have code that works; I was the only student who did. I was also the only one who put the code into a fully responsive site. I passed the homework with flying colors. But far be it to assume you know the details; the class project is long over. I'd be glad to send you my instructor's email for confirmation. I'm finishing this for myself. So, that being said, is it possible to get a hand with this?

     

    You wrote a fully responsive site, yet you can't follow the link provided about ctype_alpha and read the accompanying documentation (which has relevant and clear examples of how to use it)?

     

    ...you do realize why everyone's bullshit detector is going off, 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.