Humm, it really depends on what you're trying to do. How will you use the variables inside your class?
You could create a function inside your class which will assign the values to those variables.
<?php
class ExchangeNTLMStream extends NTLMStream {
protected $user;
protected $password;
// Alternative function 1: Pass variables when loading new object
public function __construct($myuser, $mypass) {
$this->user = $myuser;
$this->password = $mypass;
}
// Alternative 2: define a function specific to loading those values
public function set_username_and_password($myuser, $mypass){
$this->user = $myuser;
$this->password = $mypass;
}
// "$this" will set the global variables
}
// To use alternative 1:
$myobject = new ExchangeNTLMStream($username, $password);
// To use alternative 2:
$myobject = new ExchangeNTLMStream;
$myobject->set_username_and_password($username, $password);
?>Thanks BishipBooYah! This did the trick.
I haven't really done any programming with OOP, so I'm glad that you were able to shed some light on this for me.
So, now that I have this working, could you explain why I need to use a function to change a variables value in a class? Is that just the way that classes work?
Thanks,
Faithful
Humm, as I understand them, as class is sort of like a closed program if you well. A class can have variables, functions, or methods (depending on the language). You declare a variable inside a class, but you'll need a function to the outside world to modify them.
But you can also link classes together through inheritance which is a more complicated.
Classes are great for compartmentalizing your code. For example, you could have a user class which validates email addresses, creates and validates profile data, performs various admin functions, or whatever. So once your class is set up, you just call that class function and makes for cleaner and nicer code. =)
Basically the reason you can't change the variables is because of the scope of the variables... there are scopes like private, protected, and public. If you have a class like this:
<?php
class MyClass
{
public $firstname;
}
# you can set it with
$class = new MyClass;
$class->firstname = 'Jonathan';But if you have a private or protected variable, thats when you get into advanced OOP programming when you want the class to maintain variables without letting other objects modify it. But when you get into advanced OOP you get to use polymorphism (http://www.devshed.com/c/a/PHP...) where you have a final class and objects below will extend the functionality.
For instance, you have a final class called UIPanel. In this you have the ability to set the position by calling UIPanel->SetPosition($x, $y); Each member that extends UI panel like so:
class MyClass extends UIPanel
{
}will automatically inherit the function SetPosition. (if the function is public or protected).
If you are just starting out with OOP, or really advanced, I would recommend KohanaPHP as it has a great OOP setup that is optimized for speed and security. Excellent framework. I have used it on many large sites that have a bunch of BW in traffic and stuff. Blazes.
Laterez.
Here seems to be my delima, I can get my script to work by entering the USERNAME and PASSWORD manually in the PHP script. But if I want it to come over in a variable, it give me an error.
This code works:
class ExchangeNTLMStream extends NTLMStream {
protected $user = 'USERNAME';
protected $password = 'PASSWORD';
}
This code fails:
class ExchangeNTLMStream extends NTLMStream {
protected $user = $ntusername;
protected $password = $ntpassword;
}
I'm really not familiar with how to use Classes inside of PHP, but I really need this to work and for multiple users not just one.
Can anyone explain how I can $user and $password dynamically?
Thanks!
Faithful