Jump to content

Class Constructors and Properties


SystemOverload

Recommended Posts

I'm doing some basic object coding to get my head around OOP PHP, but I've come across something I can't get to work.

 

I've got two identical peices of code, MyClass is defined, MyClass2 is then defined as an extension of MyClass.  MyClass has three properties Public, Private and Protected.

 

In my first example I can access Public and Protected via a method in MyClass2, this class does NOT have a constructor.  The second example has a constructor but I cannot access Public:

 

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<body>
<?php
ECHO "MyClass2 does not have a constructor, Public & Protected both echo correctly from MyClass2->printHello!!!<BR>";
/**
* Define MyClass
*/
class MyClass
{
    public $public;
    protected $protected;
    private $private;
protected $varXXX;

function __construct() {
	$this->public = "Public";
        $this->protected = "Protected";
        $this->private = "Private";	
	//$this->printHello();
}


    function printHello()
    {
	echo $this->public . "!!!<BR>";
        echo $this->protected . "!!!<BR>";
        echo $this->private . "!!!<BR>";
    }
}




/**
* Define MyClass2
*/
class MyClass2 extends MyClass
{
    // We can redeclare the public and protected method, but not private
    protected $protected = 'Protected2';
/*
function __construct() {
        echo $this->public . "#<BR>";
        echo $this->protected . "#<BR><BR>";
        //echo $this->private . "???<BR>";	
}
*/	
    function printHello()
    {
        echo $this->public . "???<BR>";
        echo $this->protected . "???<BR><BR>";
        //echo $this->private . "???<BR>";
    }
}

$obj = new MyClass();
//echo $obj->public . "<BR>"; // Works
//echo $obj->protected; // Fatal Error
//echo $obj->private; // Fatal Error
//$obj->printHello(); // Shows Public, Protected and Private

$obj2 = new MyClass2();
//echo $obj2->public; // Works
//echo $obj2->private; // Undefined
//echo $obj2->protected; // Fatal Error
$obj2->printHello(); // Shows Public, Protected2, Undefined
?>	
</body>
</html>

 

RESULTS IN:

Public???

Protected???

 

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<body>
<?php
ECHO "MyClass2 DOES have a constructor, Public does not work from MyClass2->printHello!!!<BR>";
/**
* Define MyClass
*/
class MyClass
{
    public $public;
    protected $protected;
    private $private;
protected $varXXX;

function __construct() {
	$this->public = "Public";
        $this->protected = "Protected";
        $this->private = "Private";	
	//$this->printHello();
}


    function printHello()
    {
	echo $this->public . "!!!<BR>";
        echo $this->protected . "!!!<BR>";
        echo $this->private . "!!!<BR>";
    }
}




/**
* Define MyClass2
*/
class MyClass2 extends MyClass
{
    // We can redeclare the public and protected method, but not private
    protected $protected = 'Protected2';

function __construct() {
        echo $this->public . "#<BR>";
        echo $this->protected . "#<BR><BR>";
        //echo $this->private . "???<BR>";	
}

    function printHello()
    {
        echo $this->public . "???<BR>";
        echo $this->protected . "???<BR><BR>";
        //echo $this->private . "???<BR>";
    }
}

$obj = new MyClass();
//echo $obj->public . "<BR>"; // Works
//echo $obj->protected; // Fatal Error
//echo $obj->private; // Fatal Error
//$obj->printHello(); // Shows Public, Protected and Private

$obj2 = new MyClass2();
//echo $obj2->public; // Works
//echo $obj2->private; // Undefined
//echo $obj2->protected; // Fatal Error
$obj2->printHello(); // Shows Public, Protected2, Undefined
?>	
</body>
</html>

 

RESULTS IN:

#  << Missing the property's contents

Protected2#

 

??? << Missing the property's contents

Protected2???

 

 

 

Any ideas why I cannot access the property's contents in the second example when the only difference is that it has a constructor?

Link to comment
Share on other sites

When you extend a class, if there is no constructor in the extended class, it will call the constructor of the parent.  So the variables will be assigned.  BUT, since you assign the variables in the constructor, the properties of MyClass2's protected variable will not be set, but rather will revert to "protected".

 

You can use the functions of the parent, through the child, so there is no need to remake the same functions.

 

Try this:

<?php

class MyClass
{
    public $public;
    protected $protected;
    private $private;
protected $varXXX;

function __construct() {
	$this->public = "Public";
        $this->protected = "Protected";
        $this->private = "Private";	
	//$this->printHello();
}


    function printHello()
    {
	echo $this->public . "!!!<BR>";
        echo $this->protected . "!!!<BR>";
        echo $this->private . "!!!<BR>";
    }
}

class MyClass2 extends MyClass
{
    // We can redeclare the public and protected method, but not private
    protected $protected = 'Protected2';
   function changeProtected() {
     $this->protected = 'Protected2';
   }
   
}


$class = new MyClass();

echo $class->public; //accessable directly.
//echo $class->protected;	//not accessable directly, uncomment to see it throw a fatal error.
//echo $class->private;	//not accessable directly, uncomment to see it throw a fatal error.
echo '<br />[Class1]<br />PrintHello function: ';  $class->printHello();

$class2 = new MyClass2();

echo $class2->public;
//echo $class2->protected;	//not accessable directly, uncomment to see it throw a fatal error.
//echo $class2->private;	//not accessable directly, uncomment to see it throw a fatal error.
echo '<br />[Class2 Before Change]<br />PrintHello function: '; $class2->printHello();

//Now let us change the protected variable.
$class2->changeProtected();

echo '<br />[Class2 After change]<br />';
$class2->printHello();

?>

Link to comment
Share on other sites

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.