beginner oop: class not alway working
hello,
i am trying to learn oop, so i made a class that generates passwords.
there are several arrays and from some or each array there are elements
picked(at random) that
form the password.
the problem is: when i make an object the password shows.
but when i reload the page several times and/or changing the values that
form the
password (bv changing $test -> value = true, in , $test -> value -> false)
than nothing happens.
there is no password on the page and no error message, nothing.
the page is complely blank.
is there something wrong with the class?
thanks,
------------------------ code to show password -----------
// make an object;
$test = new WachtwoordGenerator();
// choose the amount of elements from each array;
$test -> kLetter = 8;
$test -> hLetter = 3;
$test -> cijfer = 2;
$test -> leesteken = 4;
// true means that the password contains only unique elements;
// false means that duplicates are allowed;
$test -> uniek = true;
// make the password;
$test -> maakWachtwoord();
// show the password
echo $test -> output();
------------------------- source code from the
class --------------------------------
class WachtwoordGenerator{
private $a;
private $b;
private $c;
private $d;
// amount of elements to choose;
private $aA;
private $aB;
private $aC;
private $aD;
private $wachtwoord;
// are duplicates allowed?
private $uniek;
function __construct() {
$this -> a =
array("a","b","c","d","e","f","g","h","i","j","k","l","m","n ","o","p",
"q","r","s","t","u","v","w","x","y","z");
$this -> b =
array("A","B","C","D","D","E","F","G","H","I","J","K","L","M ","N","O","P",
"Q","R","S","T","U","V","W","X","Y","Z");
$this -> c = array(0,1,2,3,4,5,6,7,8,9);
$this -> d = array("!"," [at] ","#","$","%","[","]");
$this -> wachtwoord = '';
}
function maakWachtwoord(){
// see if number is not larger than amount of elements in array;
$aantalA = sizeof($this -> a);
$aantalB = sizeof($this -> b);
$aantalC = sizeof($this -> c);
$aantalD = sizeof($this -> d);
if($this -> aA > $aantalA){
$this -> aA = $aantalA;
}
if($this -> aB > $aantalB){
$this -> aB = $aantalB;
}
if($this -> aC > $aantalC){
$this -> aC = $aantalC;
}
if($this -> aD > $aantalD){
$this -> aD = $aantalD;
}
$totaal = array($this -> a , $this -> b , $this -> c , $this ->
);
$aantal = array($this -> aA , $this -> aB, $this -> aC, $this ->
aD );
$wwRuw = array();
$aantalTotaal = sizeof($totaal);
for($y = 0; $y < $aantalTotaal; $y++){
for($x = 0; $x < $aantal[$y]; $x++){
$nummer = rand(0, sizeof($totaal[$y]) - 1 );
$teken = $totaal[$y][$nummer];
if($this -> uniek == true ) {
if(in_array($teken, $wwRuw)){
$x--;
continue;
}else{
$wwRuw[] = $teken;
}
} else {
$wwRuw[] = $teken;
}
}
}
//suffle the values;
shuffle($wwRuw);
// make a string/password
foreach($wwRuw as $k => $v){
$this -> wachtwoord .= $v;
}
}
function __set($p_sName, $p_nAmount) {
switch ($p_sName) {
case "kLetter" :
$this -> aA = $p_nAmount;
break;
case "hLetter" :
$this -> aB = $p_nAmount;
break;
case "cijfer" :
$this -> aC = $p_nAmount;
break;
case "leesteken" :
$this -> aD = $p_nAmount;
break;
case "uniek" :
$this -> uniek = $p_nAmount;
break;
}
}
function output(){
return $this -> wachtwoord;
}
}
Re: beginner oop: class not alway working
"Osewoudt" <oswwoudt [at] ufo.org> wrote in message
news:460abf73$0$30727$ba620dc5 [at] text.nova.planet.nl...
> hello,
>
> i am trying to learn oop, so i made a class that generates passwords.
>
> is there something wrong with the class?
>
> thanks,
>
>
> ------------------------ code to show password -----------
> // make an object;
> $test = new WachtwoordGenerator();
> // choose the amount of elements from each array;
> $test -> kLetter = 8;
> $test -> hLetter = 3;
> $test -> cijfer = 2;
> $test -> leesteken = 4;
> // true means that the password contains only unique elements;
> // false means that duplicates are allowed;
> $test -> uniek = true;
> // make the password;
> $test -> maakWachtwoord();
> // show the password
> echo $test -> output();
>
>
>
> $totaal = array($this -> a , $this -> b , $this -> c , $this ->
> );
The above line should throw and error. If that is the actual code, rather
than a copy paste error, it will certainly cause you some grief.
I think you meant the line below.
$totaal = array($this -> a , $this -> b , $this -> c , $this ->d );
HTH
Vince Morgan
Re: beginner oop: class not alway working
"Osewoudt" <oswwoudt [at] ufo.org> wrote in message
news:460abf73$0$30727$ba620dc5 [at] text.nova.planet.nl...
> hello,
> ------------------------ code to show password -----------
> // make an object;
> $test = new WachtwoordGenerator();
> // choose the amount of elements from each array;
> $test -> kLetter = 8;
> $test -> hLetter = 3;
> $test -> cijfer = 2;
> $test -> leesteken = 4;
> // true means that the password contains only unique elements;
> // false means that duplicates are allowed;
It appears that the class would only ever be a single instance.
If so you should be setting the values within the constructor.
ie;
$test = new WachtwoordGenerator(8, 3, 2, 4);
Although this appears to be a test and learning project it's a good time to
create some good habits.
The class should encapsulate more functionality than this one does.
The numbers "8, 3, 2, 4" have to come from somewhere. Why not generate them
within the class itself with rand numbers.
Then you could do the following;
$test= new WachtwoordGenerator();
$password = $test->pword();
Now you only have two statements involved in generating a password.
A very big part of "oop", or "obp" is data encapsulation.
There was once a time when you had to oil bits of an internal combustion
engine every time you used it. Today every such engine I know of uses
encapsulation to do the oiling for you. You just need to keep an eye on the
oil available to the system. No, I'm not calling you a "dipstick" :)
You are oiling the engine yourself when you call "_set" on a class that is
created once for it's output then discarded.
No crticism inteded here, just a nudge toward a better understanding of the
use, and usefullness, of classes.
HTH
Vince Morgan
Re: beginner oop: class not alway working
> It appears that the class would only ever be a single instance.
> If so you should be setting the values within the constructor.
> ie;
> $test = new WachtwoordGenerator(8, 3, 2, 4);
that is the way i did it in an earlier version, but then i read about __set
and __get so i wanted to use that.
> Although this appears to be a test and learning project it's a good time
> to
> create some good habits.
> The class should encapsulate more functionality than this one does.
> The numbers "8, 3, 2, 4" have to come from somewhere. Why not generate
> them
> within the class itself with rand numbers.
> Then you could do the following;
> $test= new WachtwoordGenerator();
> $password = $test->pword();
now you can control yourself how many elements there are in the password and
from which set of caracters they come.
No, I'm not calling you a "dipstick" :)
then you are the first who doesn't
> No crticism inteded here, just a nudge toward a better understanding of
> the
> use, and usefullness, of classes.
yes, that is the way i understand it and i am glad you took the time to give
some advice.
thanks,
henri osewoudt
Re: beginner oop: class not alway working
"Osewoudt" <oswwoudt [at] ufo.org> wrote in message
news:460b96a1$0$9277$ba620dc5 [at] text.nova.planet.nl...
|
| > It appears that the class would only ever be a single instance.
| > If so you should be setting the values within the constructor.
| > ie;
| > $test = new WachtwoordGenerator(8, 3, 2, 4);
|
|
| that is the way i did it in an earlier version, but then i read about
__set
| and __get so i wanted to use that.
of course you do realize that __get and __set only work on interfaces that
DON'T exist on your object, right?
ex.:
class foo
{
public $bar = 'hello world';
public function __set($name, $value)
{
trigger_error('<pre>' . $name . ' is not a valid interface.</pre>',
E_USER_WARNING);
}
}
echo foo::$bar; // no error
echo foo::$Bar; // error here
| > Although this appears to be a test and learning project it's a good time
| > to
| > create some good habits.
| > The class should encapsulate more functionality than this one does.
| > The numbers "8, 3, 2, 4" have to come from somewhere. Why not generate
| > them
| > within the class itself with rand numbers.
| > Then you could do the following;
| > $test= new WachtwoordGenerator();
| > $password = $test->pword();
|
| now you can control yourself how many elements there are in the password
and
| from which set of caracters they come.
that's the idea.
| No, I'm not calling you a "dipstick" :)
|
| then you are the first who doesn't
ah, a pessimist.
Re: beginner oop: class not alway working
"Vince Morgan" <vinhar [at] REMOVEoptusnet.com.au> wrote in message
news:460b183f$0$22843$afc38c87 [at] news.optusnet.com.au...
> Now you only have two statements involved in generating a password.
> A very big part of "oop", or "obp" is data encapsulation.
Mr. Morgan, what do you mean by "obp?"
My assumption is "object-based programming."
Thanks.
-Lost
Re: beginner oop: class not alway working
"-Lost" <missed-spam [at] comcast.net> wrote in message
news:oomdnbjy5NtjBJHbnZ2dnUVZ_qCmnZ2d [at] comcast.com...
> "Vince Morgan" <vinhar [at] REMOVEoptusnet.com.au> wrote in message
> news:460b183f$0$22843$afc38c87 [at] news.optusnet.com.au...
>
> > Now you only have two statements involved in generating a password.
> > A very big part of "oop", or "obp" is data encapsulation.
>
> Mr. Morgan, what do you mean by "obp?"
>
> My assumption is "object-based programming."
>
> Thanks.
>
> -Lost
>
>
Yes, that is what I meant. Good guess :)