class another class method in another class in php - php

I created a class named function and calling function method in another class named test. But it shows me an error -
PHP Fatal error: Call to a member function
my function class's object name is $obj = new functions();
when i call user method which is written in function class from test class I am getting an error.

class className {
function users(){
$users = "Demo User"; return $user;
}
}
$obj = new functions();
you mean this:

you can extends
class className extends test{
function __construct(){
here you add
}
}

Related

Cannot set Parent property from child class

I have view class like this:
class View {
public function __construct() {
}
public static function render($name) {
require 'views/user/header.php';
require 'views/user/'.$name.'.php';
require 'views/user/footer.php';
}
}
and I call the view class in controller like this:
class Controller {
function __construct() {
$this->view = new View();
}
}
and then I set the view property from controller child class, like this:
class Index extends Controller {
function __construct() {
parent::__construct();
$this->view->js = "test";
}
public function index() {
$this->view->render('index/index');
}
}
But when I want to get $this->js from "header.php" which is set at render function on view class, I always get this error message:
Fatal error: Uncaught Error: Using $this when not in object context
I was tried to check, Am I in the right class? using this methods in "header.php" file:
echo get_class(); // and this method return "View";
that means I was on the view class, right?
Can anyone please help me?
Thanks in advance
You have defined render() as a static method, but you are calling it as it was not static.
I would probably benefit from reading this: http://chadminick.com/articles/simple-php-template-engine.html
P.S. What you call "view" is just a template.

PHP access an object of a class in another class

I'm trying to access an object of a class in another extended class.
class MainClass{
protected $theobject;
function __construct(){
$this->theobject = new AnotherClass();
}
}
class TheClass extends MainClass{
function AnotherFunction(){
$this->theobject->SomeFunction();
}
}
I'm getting an error on $this->theobject->AnotherFunction(). The error is "Call to a member function SomeFunction() on a non-object".
But this works fine:
class TheClass{
protected $theobject;
function SoemFunction(){
$this->theobject = new AnotherClass();
$this->theobject->SomeFunction();
}
}
Is it even legal to do this in PHP?
Pretty much all the code: http://3v4l.org/MaXe6
When i var_dump the $this->theobject in TheClass is comes back as NULL.
class TheClass extends MainClass{
function __construct(){
parent::__construct();
}
function SoemFunction(){
$this->theobject->SomeFunction();
}
}
Do the construct method when init the child class.

Accessing a protected method from a parent class

I understand that I should make a method "protected" when I want it to be only available to all classes that extend the current class as well as the current class.
Okay, grandChildClass::method2() should be protected since grandchild is extended from child.
But what should it be if accessed from a parent class such as parentClass::method2()?
class parentClass
{
public function method1() {$this->method2();}
}
class childClass extends parentClass
{
protected function method2() {}
}
class grandChildClass extends childClass
{
public function method3() {$this->method2();}
}
If you try to
$p = new parentClass;
$p->method1();
You'd get a fatal error for undefined method.
Fatal error: Call to undefined method parentClass::method2() in ... on line ...
However, this will work fine:
$c = new childClass;
$c->method1();
$g = new grandChildClass;
$g->method1();
$g->method3();
All of these will call method2 as defined on childClass.

My extended CodeIgniter 2.0.2 class doesn't see it's parent class methods

This is my first OOP php app and I'm getting a little stumped here...
I created the following class that extends the CI_Model
class LXCoreModel extends CI_Model{
function __construct() {
parent::__construct();
}
public function elementExists($table,$row,$data){
$result = $this->db->select('*')->from($table)->where($row, $data)->get()->result();
if(empty($result))return false;
return true;
}
}
And here is the class extending the class above:
class LXAccAdminModel extends LXCoreModel{
function __construct()
{
parent::__construct();
}
function addAccountStatus($statusId=NULL, $username=NULL){
if($statusId==NULL)$statusId = $this->input->post('accountStatusId');
if($username==NULL)$username = $this->input->post('username');
if(elementExists('accounts','username',$username))
if(elementExists('statuses','id',$statusId))
{$this->db->insert('accountstatus',array('statusid'=>$statusId,'username'=>$username)); return true;}
return false;
}
}
Both classes are in the Model diretory, and the class LXCoreModel is autoloaded (the line $autoload['model'] = array('LXCoreModel'); exists in the autoload.php file) and yet, when I try to run my code I get this error:
Fatal error: Call to undefined
function elementExists() in
C:\wamp\www\CI_APP\application\models\LXAccAdminModel.php
on line 25
Thanks for your time! :)
You're calling elementExists(), but not as a method of the class.
Try:
$this->elementExists();
Or from LXAccAdminModel:
parent::elementExists();
$this->elementExists() should suffice in both cases, $this referring to the current class.
if i am not wrong then the error is in your derived class you have forgot to put $this while calling the elementExists() function that should be $this->elementExists()

Is there a way to assign a member variable to an object of a class without a constructor?

I want to do something like the following:
class SomeOtherClass {}
class Test
{
public $member = new SomeOtherClass();
}
The only problem is that I do not want to use a constructor, because the 'Test' class should extend another class and should not override the constructor.
Is this actually possible in PHP?
You can extend parent constructor in Test like this:
class Test extends SomeClass
{
public $member;
function __construct() {
$this->member = new SomeOtherClass();
parent::__construct();
}
}

Categories