Classes Confusion

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
tecktalkcm0391
DevNet Resident
Posts: 1030
Joined: Fri May 26, 2006 9:25 am
Location: Florida

Classes Confusion

Post by tecktalkcm0391 »

Ok, I was trying to learn how to do classes and was using this tutorial: http://www.spoono.com/php/tutorials/tutorial.php?id=27
can someone look over its codes because i can't get anything to work...

This is what I came up with so far...
I want to pass the $_POST['username'] into the class to check it and everything under the function login
(is that a security risk using it as a class variable before checking it ?)

Code: Select all

 
    class master {
        var $loginTime = time();
    
    
    
    }
    
    class login extends master {
        function login($loginUsername, $loginTime) {
            $loginText = "result: ".$loginUsername." time: ".$loginTime;
                        return $loginText;
        }
    }
    
 
$echo = new login($_POST['username']);
echo $echo;
 
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: Classes Confusion

Post by John Cartwright »

You cannot echo an object unless you implement the __toString() function (which you will learn about furthur down the road).

In php4, the constructor is the name of the class, which is called when the class is initialized. Constructors cannot return anything. What you want to do is create another method to retrieve the data.

Secondly, you access the class properties using $this. I.e., $this->loginTime;

Thirdly, when declaring variables you cannot call a function, i.e. var $loginTime = time(); is not valid

Fourthly, any parameters to methods you do not give a default value to are considered required. In your login::login() method you have 2 parameters, but you are only pass one. I think you only wanted to pass the first parameter.

To make your example work,

Code: Select all

   class master {
        function getTime() {
            return time();
        }
    }
   
    class login extends master {
        var $result;
 
        function login($loginUsername) {
            $loginText = "result: ".$loginUsername." time: ". $this->getTime();
            $this->result = $loginText
        }
       
        function getResult() {
            return $this->result;
        }
    }
   
$echo = new login($_POST['username']);
echo $echo->getResult();
(is that a security risk using it as a class variable before checking it ?)

Generally you should not access class variables from outside the class scope. Meaning, you should be using setters and getters to change the class properties from outside the object.

Side note, I would recommend you pick up php5 for OOP
User avatar
tecktalkcm0391
DevNet Resident
Posts: 1030
Joined: Fri May 26, 2006 9:25 am
Location: Florida

Re: Classes Confusion

Post by tecktalkcm0391 »

Thanks!

I don't get what your saying about the security thought. Is is or is not secure to do

Code: Select all

$echo = new login($_POST['username']);
echo $echo->getResult();
if the POST is validated inside the class....

Also... am I understanding this right...
I can access the functions in login above, but not by doing

Code: Select all

 
$echo = new master();
Finally, is there anyway to do the login without having to have the class name login and the function login, cause I'd like to do it so I could access the multiple login functions under one class, if you could access then from the Master Class:

Would something like this be right:

Code: Select all

$echo = new login;
$echo->loginScript($_POST['username']);
echo $echo->getResult();
Post Reply