gish Posted October 10, 2008 Share Posted October 10, 2008 hi The code below works except the construct function i get the following error if someone could tell me why that would be that would be great. But the real reason for the post is this oop best practice. I have looked a different tutorials and as I am learning oop I don't want to get into any bad habits. So is the code submitted good, bad or indifferent?? thanks gish Warning: Missing argument 1 for security::__construct() this is "security_class.php class security { var $past_string; function __construct($string_to_check) { $this->past_string = $string_to_check; } function preg_match_0to9 ($string_to_check){ if (preg_match('/^[0-9]+$/',$string_to_check)) { $tested_string = "The number you chose is ".$string_to_check; $this->past_string = $tested_string; } else{ $tested_string = "You are only allowed to add numbers ".$string_to_check; $this->past_string = $tested_string; } } function security_checked() { return $this->past_string; } } this is in the web page <?php include("security_class.php"); ?> </head> <body> <?php if (isset($_POST['senders_infomation'])== ''){ $_POST['senders_infomation'] = 'enter information'; } $string_to_check = $_POST['senders_infomation']; $script_protector = new security(); // accessing the class $script_protector->preg_match_0to9($string_to_check); // is accessing the function echo $script_protector->security_checked(); echo "<form method='post'>"; echo "<input type='text' size='30' name='senders_infomation'>"; echo "<input type='submit' value='Go'name='Go Button'>"; echo "</form>"; ?> </body> Quote Link to comment https://forums.phpfreaks.com/topic/127914-solved-is-this-corect-oop/ Share on other sites More sharing options...
Zane Posted October 10, 2008 Share Posted October 10, 2008 Well here you are clearly missing an argument $script_protector = new security(); // accessing the class have you tried putting one and see if you get the same error Quote Link to comment https://forums.phpfreaks.com/topic/127914-solved-is-this-corect-oop/#findComment-662297 Share on other sites More sharing options...
gish Posted October 10, 2008 Author Share Posted October 10, 2008 I have been looking at the script I added $script_protector = new security($string_to_check); Worked great thank you for helping me with that issue. Is the script good oop php practice is anyone able to tell me?? Quote Link to comment https://forums.phpfreaks.com/topic/127914-solved-is-this-corect-oop/#findComment-662318 Share on other sites More sharing options...
keeB Posted October 11, 2008 Share Posted October 11, 2008 I think it's a great first attempt. My only complaint (and bad habit) is not declaring public, private variables/functions so any client code can access your object directly. This is not a good idea. Quote Link to comment https://forums.phpfreaks.com/topic/127914-solved-is-this-corect-oop/#findComment-662453 Share on other sites More sharing options...
gish Posted October 11, 2008 Author Share Posted October 11, 2008 thanks I will go away and have a look how I will place public or private into my script. In new about them but I was trying to it to work Quote Link to comment https://forums.phpfreaks.com/topic/127914-solved-is-this-corect-oop/#findComment-662527 Share on other sites More sharing options...
gish Posted October 11, 2008 Author Share Posted October 11, 2008 Ok I have made some changes, I am wondering if I got the right theory right I made the variables(properties) private so that the class is the only thing that can access it,. I tried to make the functions(methods) private so that the class would be only be able to use them but i would not work kept getting errors so I made them public. I might have the theory all wrong can anyone tell me if this is correct. class security { private $past_string; private $string_to_check; private $tested_string; function __construct($string_to_check) { $this->past_string = $string_to_check; } public function preg_match_0to9 ($string_to_check){ if (preg_match('/^[0-9]+$/',$string_to_check)) { $tested_string = "The number you chose is ".$string_to_check; $this->past_string = $tested_string; } else{ $tested_string = "You are only allowed to add numbers ".$string_to_check; $this->past_string = $tested_string; } } public function security_checked() { return $this->past_string; } } Quote Link to comment https://forums.phpfreaks.com/topic/127914-solved-is-this-corect-oop/#findComment-662540 Share on other sites More sharing options...
keeB Posted October 11, 2008 Share Posted October 11, 2008 That is correct. Generally, you will probably never need to make private methods. But, they are accessible to the class only (as you thought). Here's a code example: <?php class A { private $b = "hi"; } $a = new A() print $a->b; //doesn't work because b is private class C { private $a; public setA($foo) { $this->a = $foo; } } $c = new C(); $c->setA('hello!'); // this does work! /// now lets mix what we know about public and private and make a bit more complex object class D { private $a; public setA($foo) { $this->doSomethingWithA($foo); } private doSomethingWithA($foo) { $this->a = substr($foo, 0, 5); } public function getA() { return $this->a; } } $d = new D(); $d->setA('Something long string...........'); print $d->getA(); // returns 'Somet' ?> Quote Link to comment https://forums.phpfreaks.com/topic/127914-solved-is-this-corect-oop/#findComment-662947 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.