Jump to content

Recommended Posts

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>


Link to comment
https://forums.phpfreaks.com/topic/127914-solved-is-this-corect-oop/
Share on other sites

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;
   }
}

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'

?>

 

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.