开发者

Class Inheritance Problem

I have A.php and B.php

A.php

<?php
error_reporting(E_ALL);
ini_set("display_errors",1);
class myClass
{
  function hello()
  {
    return 'hello';
  }  
}
?>

B.php

<?php
error_reporting(E_ALL);
ini_set("display_errors",1);
require_once('/A.php');
$a = new myClass();
testing();

function testing()
{    
    echo $a ->hello();
}
?>

B.php in开发者_开发问答herits A.php , if i run B.php,but it show "Fatal error: Call to a member function hello() on a non-object."

So the question is simple, how can i correct this ,but "$a = new myClass();" is not inside the function, since in .NET world can do this, i believe PHP also possible.

And one more question is, what is the modify of function in A.php ,if i have not state private/public/protected?


This is not inheritance. B.php merely includes A.php. This means all the code in A.php becomes available to the execution context of B.php.

Inheritance is a class relationship

class A{}

class B extends A{}

Class B can be said to inherit from A. So, a hierarchy has been formed.

Much as we humans inherit traits from our parents, classes inherit properties from their parents. For this reason, it is very common to talk about these classes in terms of their place in the hierarchy. Class A is a parent to class B. Class B is a child to class A.

But the actual problem you are having, the source of the error message, is a scope issue. You create $a in the global scope but attempt to access it from within a function's scope, in this case testing().

The best way to solve this is to pass your instances of myClass into testing()

$a = new myClass();
testing( $a );

function testing( $myClassObj )
{    
    echo $myClassObj->hello();
}

And to answer your final question - in PHP5 - class members not explicitly declared with an access modifier become implicitly public.


If you want to use $a in the function, you need to either put

global $a;

as the first line of the function, or access it as $GLOBALS['a']


use this

function testing()
{   
    global $a;
    echo $a ->hello();
}


A couple of things I would change here, I'll explain in a moment.

A.php

<?php
error_reporting(E_ALL);
ini_set("display_errors",1);

class myClass
{
  // Here, access modifier.
  public function hello()
  {
    return 'hello';
  }  
}
?>

B.php

<?php

error_reporting(E_ALL);
ini_set("display_errors",1);

require_once('/A.php');
testing();

function testing()
{   
    // Here, change where variable is defined to place it in scope.
    $a = new myClass();
    echo $a ->hello();
}
?>

When no access specifier is given on a method, it defaults to public. However, it is generally better to just declare what you want the method to have. You will appreciate this if you find yourself actively coding in multiple languages as they will each have a different default.

Right now the variable $a is not in the scope of the function testing(). Allow me to rearrange your program and you shall see why. You could have written it, like so:

B.php

<?php
function testing()
{       
    echo $a ->hello();
}    

error_reporting(E_ALL);
ini_set("display_errors",1);

require_once('/A.php');
$a = new myClass();
testing();

?>

You see, where testing() is now defined, $a does not exist. It hasn't been defined yet. So it is not inside testing()'s scope. You'll have to either define $a inside testing() or else pass it in as a parameter. In my first pass, I changed your code to define $a inside testing(). If you will need to use it in multiple functions, then I would suggest changing testing() to take it as a parameter. Like this:

function testing(myClass $a) {
    echo $a->hello();
}

Then pass it in this way:

$a = new myClass();
testing($a);


I believe $a is not defined in the function. Try using global $a first, and then calling hello().

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜