Ahmad Khan
Ahmad Khan

Reputation: 529

PHP class extends a public variable

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();

Upvotes: 1

Views: 1498

Answers (2)

ddinchev
ddinchev

Reputation: 34673

From what I see - in the case where the flag is set, something has set it - there is no default value. The problem is not in the extending of validate, you just don't set the public property to any value before accessing it in InsertINDB. Eg in the first case ValidateTypeOfMedia is called, in the second, not.

edit: OR $SOEma == "" in the second case evaluates to true and the ValidateTypeOfMedia does not set the flag to 1.

BTW imho your code does not look right, not best practice, whatever you are trying to achieve (by no means trying to insult you).

edit2:

What is the result from those, I bet in both cases flag is not set:

$object_validate= new validate;

$object_DB= new InsertINDB;

$object_validate->whatever();

$object_DB->test();

edit3:

Khanquered Pro, based on your comment - you maybe do not understand the purpose of polymorphism. So to explain - after you extend the first class, you create an instance of the child (in your case InsertINDB). Then that instance holds all the methods (functions) and properties of validate. You can not first call validate in one instance, then expect its properties to be available in the child class - every instance is completely separated thing in this case, they have own property values (state). So after you extend validate you only work with the child - use it to validate, use it to insert to db. And you use the partent validate only in cases in the application where you need to validate data without inserting anything.

I will not comment on if this whole architecture is good idea - its beyond the scope of the question and probably your current knowledge, but after you succeed with your task, read more on the topic of OOP as a start.

Upvotes: 2

DaveRandom
DaveRandom

Reputation: 88647

Your problem here is a misunderstanding of how extending classes works, and the difference between classes and objects.

When you do $var = new ClassName you 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:

class Bob {

  public $property = 1;

}

class Dave extends Bob {
}

$bob = new Bob;
$dave = new Dave;

echo $bob->property; // 1
echo $dave->property; // 1

$bob->property = 2;

echo $bob->property; // 2
echo $dave->property; // 1

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.

Upvotes: 2

Related Questions