I have two classes
class validate {
public $mediaFlag;
function ValidateTypeOfMedia($SOEmag,$SOEtab,$Soct,$DAL,$insertMeda,$other){
if($SOEmag==""){
return "Must select Media Type";
}
else{
$this->mediaFlag=1;
}
}
function whatever()
{
if( $this->mediaFlag==1)
{
echo "flag is here";
}
else {
echo "flag didn't work";
}
}
}/// Class validate Ends
class InsertINDB extends validate
{
function test(){
if( $this->mediaFlag==1)
{
echo "flag is here";
}
else {
echo "flag didn't work";
}
}
}
The problem I am having is in class insertINDB , function test does not recognize that the mediaFlag variable has been set…however, function whatever in the parent class does recognize so. so my question, how come function test in class InsertINDB does not know that the flag has been set in the parent class.
$object_validate= new validate;
$object_DB= new InsertINDB;
$object_validate->whatever();
$object_DB->test();
Your problem here is a misunderstanding of how extending classes works, and the difference between classes and objects.
When you do
$var = new ClassNameyou instantiate an object based on the class. This object is no longer connected to the class directly, and changing a value in another instance from the same class does not affect the other instances.The same principal applies to classes which extend other classes – once instantiated, an object stands separate from every other object, regardless of whether they are from the same class or one of it’s parents/children.
Let’s take a simple example:
The behaviour you are expecting can be obtained by using static members (also sometimes known as class members).
I think it would be worth you going back to the manual and re-reading the OOP chapter in full.