Jump to content

[SOLVED] How does a 'protected' variable change the behavior of the object?


kratsg

Recommended Posts

Example (use the following class to illustrate my question)

 

<?php

class User {

protected $username;

function __construct($username){
	$this->username = $username;
}

function getUser(){
	return $this->username;
}

function setUser($username){
	$this->username = $username;
}

}

//example in echoing variables
$someObject = new User('Billy');
echo $someObject->$username;
echo $someObject->getUser();


//example in setting variables
$someObjects = new User('Bob');
$someObjects->$username = 'Billy';
$someObjects->setUser('Billy');
?>

 

What will happen with each case?

<?php

class User {

protected $username;

function __construct($username){
	$this->username = $username;
}

function getUser(){
	return $this->username;
}

function setUser($username){
	$this->username = $username;
}

}

//example in echoing variables
$someObject = new User('Billy'); //userame will be set
echo $someObject->$username; //would not echo - error will either be fatal or a warning (cant remember which)
echo $someObject->getUser(); //this will echo because your class method is accessing the object variable (not from externally)


//example in setting variables
$someObjects = new User('Bob'); //will work
$someObjects->$username = 'Billy'; //will not work and error
$someObjects->setUser('Billy'); //will work
?>

 

Protected allows access to a variable/method ONLY from the parent class or one of its children. This means you will not be able to access it via creating an object of the class.

 

The general rule is "set all object variables to private" if you need to access it from another class set it to protected. It is generally frowned upon to have a public object variable because anything can be passed into your class allowing for errors and exploits. Use a method like you have within your class to set a variable this way you control how the data is entered and it can only enter in the format you wish.

 

for instance

<?php

class User {

private $username;

public function __construct($username){
	$this->setUser($username);
}

public function getUser(){
	return $this->username;
}

private function setUser($username){
	if (preg_match("/^[a-zA-Z0-9]{4,10}$/", $value))
		{		
			$this->username = $username;
			return 1;
		}
	return 0;
}

}

//example in echoing variables
$someObject = new User('Billy'); //userame will be set
echo $someObject->$username; //would not echo - error will either be fatal or a warning (cant remember which)
echo $someObject->getUser(); //this will work


//example in setting variables
$someObjects = new User('Bob!!'); //will not work
$someObjects->$username = 'Billy'; //will not work and error
$someObjects->setUser('Bob '); //will not work and error
?>

Ok, how do you learn about this specific stuff, such as static, public, private, protected.. Usually, I can get coding easily, but when it comes to comprehension of ideas, that's where it gets tricky for meh XD

 

What you said makes sense though. So, let's say a class extended user, how would that child be able to access the protected variable?

I got this book and it all unfolded from there:

http://www.amazon.com/PHP-Objects-Patterns-Practice-Second/dp/1590599098/ref=pd_sim_b_1

 

Also php.net

http://uk2.php.net/zend-engine-2.php

 

OOP isn't like learning to code the logic is completely different but once you get the hang of it you will never go back.

Archived

This topic is now archived and is closed to further replies.

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