Method undefined in child class - php

<?php
abstract class file
{
private $pid;
private $uid;
public function __construct($pid,$uid)
{
$this->pid = $pid;
$this->uid = $uid;
}
public function valid()
{
if($_SESSION['level']<$this->pid)
{
return true;
}
else
return false;
}
public function allow()
{
return "This is all right!";
}
}
?>
<?php
// put your code here
include("../file.php");
session_start();
class android extends file
{
public function __construct($pid,$uid)
{
parent::__construct($pid, $uid);
}
}
$uid = $_SESSION['id'];
$pa = new android(1,$uid);
if($pa->valid())
echo $pa->allow();
else
echo "<h1>No permission<h1>"
?>
The above class is android class and the one above that is file.. Now when the android extends(inherits) the file class, it means it has all the methods. But when I try to run the program, it says undefined variable android::allow()
I dont understand because I have defined the allow() function in the file class and so the android class should inherit the method as well.
please help.. Thanks in advance.

According to http://www.php.net/manual/en/language.oop5.abstract.php ,
Classes defined as abstract may not be instantiated
If your file class doesn't have any abstract method, don't bother declaring it as abstract, otherwise avoid calling the constructor ;)

Related

PHP Object created in __construct Not Accesible inside function of same Class

PHP website developed using MVC pattern. i want access an object created using constructor in all functions of class. when i check is_object its not there so i create object one more time.
When try to simulate the MVC pattern its working fine(below code) getting Object Accessible: $this->ldap Logged In!. But in my actual website getting as Non-Object: $this->ldap Logged In!
What could be possible mistakes i done? is there anyway to find what
is the issue?
class ldap{
function ldap_userAuth(){
return TRUE;
}
}
class commonfunctions {
private $ldap;
public function __construct(){
$this->ldap=new ldap();
}
function userLogin(){
if(!is_object($this->ldap)){
$this->ldap=new ldap();
echo 'Non-Object: $this->ldap'.PHP_EOL;
}
else{
echo 'Object Accessible: $this->ldap'.PHP_EOL;
}
if($this->ldap->ldap_userAuth()){
return TRUE;
}
else{
return FALSE;
}
}
}
class Model extends commonfunctions {
}
class Controller {
public $model;
public function __construct(){
$this->model = new Model();
}
public function invoke(){
if($this->model->userLogin()){
echo 'Logged In!'.PHP_EOL;
}
else{
echo 'Logged Out!'.PHP_EOL;
}
}
}
$controller = new Controller();
$controller->invoke();
Note:- classes are included in the above order for my actual website.
Above Code snippet is working as expected i have issue only in live website.
Try calling the __construct method of the parent in the Model class like this:
class Model extends commonfunctions {
public function __construct(){
parent::__construct();
}
}
to make sure that the constructor is being called.

Acces variable set in parent constructor from child function

There are already many threads about this kind of problem, but for some reason i can't get it to work.
In TestClass::test(), $db is NULL.
The $db value is set in App construct and I'm trying to recover that value from an extended class function. (so i don't need to set $db everytime everywhere).
Some help would be greatly appreciated, thanks.
File : index.php
<?php
include('classes/App.class.php');
$oApp = new App();
echo TestClass::test();
?>
File : App.class.php
<?php
class App {
protected $db;
public function __construct () {
include_once("CAutoLoader.class.php");
$oCAutoLoader = new CAutoLoader();
$this->db = "someValue";
}
}
?>
File : TestClass.class.php
<?php
class TestClass extends App
{
function __construct () {
}
public static function test () {
return $db;
}
}
?>
File : CAutoLoader.class.php
<?php
class CAutoLoader {
CONST CLASS_EXTENSION = '.class.php';
public function __construct () {
spl_autoload_register(array($this, 'loader'));
}
private function loader ($className) {
include $className . self::CLASS_EXTENSION;
}
}
?>
You forgot a this in your TestClass and a static method cannot access non-static properties. Remove the static keyword and return the right value.
public function test() {
return $this->db;
}
Edit:
If you meant to retrieve the instance of the db via a static method you must declare the variable as static.
class App {
protected static $db = 'hey';
...
}
class TestCase extends App {
public static function test() {
return parent::$db;
}
}
echo TestCase::test(); // returns hey

PHP - Declare function in class based on condition

Is there a way to do something like this:
class Test {
if(!empty($somevariable)) {
public function somefunction() {
}
}
}
I know this might not be best practice, but I need to do this for a very specific problem I have, so is there anyway to do it?
I just want that function to be included in the class if that variable (which is tied to a URL param) is not empty. As it is written now, I get Error: syntax error, unexpected T_VARIABLE, expecting T_FUNCTION
Thanks!
It depends on the your specific use case, and I don't have enough info to give a specific answer, but I can think of one possible fix.
Extend the class, using an if statement. Put everything except the one function in AbstractTest.
<?php
abstract class AbstractTest
{
// Rest of your code in here
}
if (!empty($somevariable)) {
class Test extends AbstractTest {
public function somefunction() {
}
}
} else {
class Test extends AbstractTest { }
}
Now, the class Test only has the method somefunction if $somevariable isn't empty. Otherwise it directly extends AbstractTest and doesn't add the new method.
Call the required function if the variable is not empty.
<?php
class Test {
public function myFunct() {
//Function description
}
}
$oTest = new Test();
if(!empty($_GET['urlParam'])) {
oTest->myFunc();
}
?>
class Test {
public function somefunction() {
}
}
is all you need actually.
Please note that a function inside a class is called 'method'.
AFAIK you cannot have a condition out of the method in class scope (if that flows)
Class Test {
if (empty($Var)){
public function Test_Method (){
}
}
}
Will not work. Why not have it constantly exisisting but only call the method when it's needed?
Example:
Class Test {
public function Some_Method(){
return 23094; // Return something for example purpose
}
}
Then from your PHP:
$Var = ""; // set an empty string
$Class = new Test();
if (empty($Var)){
echo $Class->Some_Method(); // Will output if $Var is empty
}
Perhaps you trying to validate a string within OOP scope, then take this example:
Class New_Test {
public $Variable; // Set a public variable
public function Set(){
$This->Variable = "This is not empty"; // When calling, $this->variable will not be empty
}
public function Fail_Safe(){
return "something"; // return a string
}
}
Then out of Scope:
$Class = new New_Test();
if (empty($Class->Variable)){
$Class->Fail_Safe();
} // Call failsafe if the variable in OOP scope is empty

How to call class A method inside class B method and to make class B methods and parameters available from class A method?

class User
{
private $_var_1 = 10;
private $_var_2 = 20;
private function _preExecute() {
//do something here before executing sub-class's method
}
private function _postExecute() {
//do something here after executing sub-class's method
}
private function _anyMethod() {
echo "Hello!";
}
public function execute($action) {
$this->_preExecute();
$obj = new __CLASS__."_".$action;
$obj->execute();
$this->_postExecute();
}
}
class User_Add
{
public function execute() {
echo $this->_var1;
echo $this->_var2;
parent::_anyMethod();
}
}
$user = new User();
$user->execute("Add");
So, as you can see I want to be able to access User class's variables and methods from class User_Add, is it possible in PHP 5.2 or higher?
Sure!
class User_Add extends User
But your functions and variables in the User class have to be protected or public to be accessible for the User_Add class. Take a look at inheritance in the php documentation.

Is there a way define a global variable which is accesible from a class method?

Here is the situation
I create a instance of a class
$newobj = new classname1;
Then I have another class writtern below and I want to this class to access the object above
class newclass {
public function test() {
echo $newobj->display();
}
}
It is not allowed, is there a way define a variable globally through a class?
It is allowed, but you need to use the appropriate syntax: either the global keyword or the $GLOBALS superglobal:
http://es.php.net/manual/en/language.variables.scope.php
http://es.php.net/manual/en/reserved.variables.globals.php
<?php
class classname1{
private $id;
public function __construct($id){
$this->id = $id;
}
public function display(){
echo "Displaying " . $this->id . "...\n";
}
}
$newobj = new classname1(1);
class newclass {
public function test() {
global $newobj;
echo $newobj->display();
}
public function test_alt() {
echo $GLOBALS['newobj']->display();
}
}
$foo = new newclass;
$foo->test();
$foo->test_alt();
?>
However, global variables must always be used with caution. They can lead to code that's hard to understand and maintain and bugs that are hard to track down. It's normally easier to just pass the required arguments:
<?php
class classname1{
private $id;
public function __construct($id){
$this->id = $id;
}
public function display(){
echo "Displaying " . $this->id . "...\n";
}
}
$newobj = new classname1(1);
class newclass {
public function test(classname1 $newobj) {
echo $newobj->display();
}
}
$foo = new newclass;
$foo->test($newobj);
?>
Last but not least, you might be looking for the singleton OOP pattern:
http://en.wikipedia.org/wiki/Singleton_pattern
Make the instance a global:
$newobj = new classname1;
class newclass {
public function test() {
global $newobj;
echo $newobj->display();
}
}
Since I got a downvote for the first line, I removed it.
you can also inherit your newclass from classname1 and then you will be able to call the methods of the class 'classname1'
like this:
class newclass extends classname1 {
public function test() {
echo parent::display();
}
}
Could look at using the singleton pattern.
This basically is a class that creates an instance of itself and subsequent calls will access this instance and therefore achieving what I think you might be up to...

Categories