Re: working with class inheritance

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Jeff Taylor wrote:
Hey all, got a slight problem, where for some reasons my variables dont seem
to be getting stored in the child class:

e.g

class Parent
At least in PHP 5.2.1 on windows xp (for testing only), the class name Parent is a reserved class name, you cannot define a class by that name.

{
  $private type;

  public function __construct()
  {
  }

   public function GetType()
   {
      return $this->type;
  }
}

class Child implements Parent
{
    $public function __construct()
public should not be prefixed with '$'
you are missing your open '{' for this method


You need to call
parent::__construct();


      $this->type= 'Child';
      }
}

$Child= new Child();
echo $Child->getType;
also, you are calling a method, not accessing a property.

it should be
echo $Child->getType();


Can u see any reason why the type would return null?

Thanks,

Jeff


well, for starters, I think it is incorrect to have public and private prefixed with a '$'. But then you have it on some, but not all of them.....

Besides that, does it give you an error? What does it return if it doesn't return 'Child'??

What version of PHP are you running on. there is a differnet way the constuct() method gets called.

public and private are not available in versions older than php 5

in PHP version < 5 you have to have a method named the same as the class that you are creating.

Try this:
<?php

class myParent {
#	private $type;	#<--- With this, I cannot echo $type

	var $type;	#<--- With this, I CAN echo $type

	public function __construct() {
	}

	public function myParent() {
		$this->__construct();
	}
	public function getType() {
		echo $this->type;
	}
}

class myChild extends myParent {
	public function __construct() {
		parent::__construct();
		$this->type = 'Child';
	}
	public function myChild() {
		$this->__construct();
	}
}

$Child = new myChild();
echo $Child->getType();

?>

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux