Re: Re: OOP - Calling methods from classes that are inheriting?

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

 



Maciek Sokolewicz schreef:
class myClass {
    public function func() {
        return "Yay!!";
    }
}

class otherClass extends myClass {
    public function otherFunc() {
        echo $this->func();
    }
}

$class=new otherClass();
echo $class->otherFunc();

oh yes, it's that simple.
2 things to remember:
1. If you want to see output, actually make sure the function you call RETURNS any output in the first place. ($hello = "Yay!!"; isn't returning anything) 2. parent::* calls on the parent class in a static context. If your class extends another, it automatically inherits all functions from that class, unless it overwrites it itself. So you can simply use $this->func() in your child class.


not true (well you can use $this->func() in this case) ... but
parent::* assumes the context your in (i.e. static or not):

<?php

class Test
{
	static function func1() { echo __METHOD__,"\n"; }
	function func2() { echo __METHOD__," called from ", get_class($this), "\n"; }
}

class TestTwo extends Test
{
	static function func1() { parent::func1(); }
	function func2() { parent::func2(); }	
}

$t2 = new TestTwo;

TestTwo::func1();
$t2->func2();



Ben Stones wrote:
Hi, maybe if I post below what I'm trying to do it may make more sense:

class myClass {
        public function func() {
            $hello = "Yay!!";
        }
    }

    class otherClass extends myClass {
        public function otherFunc() {
            echo parent::func();
        }
    }

    $class=new otherClass();
    echo $class->otherFunc();

Nothing outputs. Sorry I am slightly new to OOP so there may be a simple fix
for this?

use a return statement, just like standalone functions, assuming
you want to output the "Yah!!".


2008/9/21 Lupus Michaelis <mickael+php@xxxxxxxxxxxx<mickael%2Bphp@xxxxxxxxxxxx>

Ben Stones a écrit :

Hope I have made myself as clear as possible!

 I did'nt understand what you mean, but I guess you're seeking for the
parent keyword. Read again the PHP manual about OOP.

--
Mickaël Wolff aka Lupus Michaelis
http://lupusmic.org

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







--
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