Your question is based upon the misunderstanding of the meaning of the term typecasting. The PHP Manual's page on type casting and the whole manual in general, is inadequate and is not a self-contained canonical reference for learning the php language. Plus, it assumes you to have knowledge of other programming languages such as C.
Typecasting is not defined as the data-type conversion of a variable; it is the data-type conversion of an expression -- In general, that is in most programming languages. Wikipedea defines it as following:
In computer science, type conversion, type casting, type coercion, and type juggling are different ways of changing an expression from one data type to another.
The official manual uses three terms namely, type juggling, type conversion and type casting. It can be guessed from the first paragraph that type juggling and type conversion are one and the same things. In the first paragraph they say:
Note that this does not change the types of the operands themselves; the only change is in how the operands are evaluated...
It should be clear that type juggling definitely doesn't change the type of the variable. From the php manual it appears as type juggling and type casting are two different concepts. The question is, since The PHP manual never defines these terms, how can we make sure if those two terms are same and what they actually mean. In the Type Casting article the manual says:
Type casting in PHP works much as it does in C:...
So, the answer is, we can safely assume that the definition of type casting from the C language applies to the PHP language. In C language type casting is defined same as the Wikipedia definition, that is only the expression's data type is converted. The following excerpts are taken from the book The C Programming Language by K&R, 2nd edition, section 2.7, page 45:
In the construction (type-name) expression
, the expression is converted to the named type... ...The precise meaning of cast is as if the expression were assigned to a variable of the specific type... ...we can use sqrt((double) n)
... ...Note that cast produces the value of n in the proper type, n itself is not altered.
This concludes the fact that type casting in php works the same way as type juggling in that the data type of variables(operands) being acted upon is not changed. You can rather use the function settype()
to convert the data type of a variable.
As pointed out in the first paragraph, php manual's page on types gives the following technically wrong comment in their last paragraph:
To forcibly convert a variable to a certain type, either cast the variable or use the settype() function on it.
Now, you know what type casting in php actually means and why you had that misconception, it would be better to rephrase your question as following:
Rephrased question: How to permanently convert the data type of class properties in PHP.
It should be obvious that public properties will be converted to different type easily by settype($myObj->myPubProp, required-type)
. The interesting thing is, contrary to the suggestion made in user yivi's original answer, privated
and protected
properties can be assessed and can have their type converted from outside the class[1][2].
Method 1: Using references:
class myClass {
private $prop = 786; //Could be protected too.
public function &assess_priv(){
return $this->prop;
}
public function display_prop() {
echo var_dump($this->prop);
}
}
$obj = new myClass;
$newObjProp = &$obj->assess_priv();
settype($newObjProp, "string");
$obj->display_prop(); //converted the data type of private property of a class
Method 2: Using PHP property overloading
error_reporting(E_ALL);
class myClass {
private $prop = 786; //Could be protected too.
public function __set($name, $value)
{
$this->$name = $value;
}
public function __get($name)
{
return $this->$name;
}
}
$obj = new myClass;
var_dump($obj->prop);
$obj->prop = (string)$obj->prop; //Interestingly, settype($obj->prop, "string"); can't be used
echo "</br>";
var_dump($obj->prop); //converted the data type of private property of a class