PHP __autoload() function not working [closed] - php

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
i want to use __autoload() but it is not working here is the code
function __autoload($class)
{
require_once($class.".php");
}
and here create object
$obj = new MyClass();

Try this code.
function __autoload($class)
{
require_once($class.".php");
}
spl_autoload_register('__autoload');
$obj = new MyClass();

Try this. it works for me.
if (!function_exists ("__autoload"))
{
function __autoload($classname)
{
require_once($class.".php");
}
}

Related

Not working Lang Changer in a Class Function PHP [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
Hello i try to implent a Language Changer in my Script.
But in my Class Function it is saying:
Notice: Undefined variable: lang in ......
I use a if else at begin of my Index.php which decide to includes the correct lang_xxx.php.
And in my lang_xx.php there is Array
/*
-----------------
Language: English
-----------------
*/
$lang = array();
$lang['welcome_msg'] = 'Welcome Guest';
And echo in my Class then
<?php echo $lang['welcome_msg']; ?>
So my Question why isnt this working and what is the correct way to implent this?
Its also not working when i include the lang_xx.php File directly in the Class Function.
Use 'global' to declare that the variable exists outside of the class.
$lang = array();
$lang['welcome_msg'] = 'Welcome Guest';
class Foo {
function bar(){
global $lang; // declare $lang as global, inside the method
echo $lang[]...
}
}

Call function inside class [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I'm trying to create a simple class in PHP, but i've got some trouble with a method call.
<?php
include('MySQL.php');
class User {
var $sql;
function _construct(){
// SQL connection
$this->sql = new MySQL(<<hidden>>, <<hidden>>, <<hidden>>);
}
public function login($username, $password){
// TODO
}
}
?>
At the //TODO section, i want to do a call like $this->sql->select('users'), but it won't let me do it. It gives an error and says that sql is a non-object.
Your "_construct" is missing a _ (you must have two), to it won't get called. Change it to :
public function __construct(){
It should work. Also remember to make it public.
If it's not called, you $sql variable isn't initialized and is actually a non-object for PHP.
Also, you may precise the visibility of your variable when declaring it, rather than using the deprecated var keyword :
private $sql;

PHP: don't want to show __construct or any variable inside the _construction function [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 8 years ago.
Improve this question
I created a class with a constructor (__construct()), but I don't want anyone to be able to access it. How can I do that? Thank you very much!
Edit 1:
For more detail: I created a class:
<?php
class test{
function __construct()
{
$a=1;
}
}
$t = new test;
$t->//here's the problem
?>
In my editor, when press $t->, the code hint shows the ('_construct()') and ('$a') too.
I want to ask: Can someone else can acces ('$a') or ('_construct()').
How can I prevent that,
Just make the constructor private
class Test {
private function __construct() {}
}
If you don't let any access the constructor function for your class - no one will be able to use that class as they will not be able to instantiate it.
In any case, if they have your class file they will be able to look at the source code and see the constructor.

Array of Objects inside another object - PHP [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
My problem is below:
class AClass{
BClass objB;
CClass objC = array();
}
$objC1 = new CClass();
$objC1->x = data; .....
$objA1 = new AClass();
$objA1->objC[] = $objC1;
So what i want to do is, there is an array of CClass objects, which should go inside AClass.
Tries arrayobjects, push etc. no luck.
Thanks in Advance.
As mentioned in my comment, PHP does not support typed class properties. I would control access to the objC property via methods which can have typed arguments. For example
class AClass {
private $objB;
private $objC = array();
public function addC(CClass $obj) {
$this->objC[] = $obj;
}
}
$objA1 = new AClass;
$objA1->addC($objC1);

Need Help Converting .NET class structure to PHP [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I was hoping someone can help me achieve the same structure in php. Here is what I do in my .NET class.
Public Class objSample
Private _MyPrivateVar As Boolean = False
Public Property MyPublicProperty As Boolean
Get
Return _MyPrivateVar
End Get
Set(ByVal value As Boolean)
_MyPrivateVar = value
End Set
End Property
End Class
Here it is:
class objSample
{
private $_MyPrivateVar = false;
public $_MyPublicProperty;
public function getMyPrivateVar()
{
return $this->_MyPrivateVar;
}
public function setMyPrivateVar($val)
{
$this->_MyPrivateVar = $val;
}
}
I have no idea what's the purpose of this code, having lack of VB knowledge, it seems it has practically non use. However, I tried to make it as I think it has to work. Maybe someone more experienced with Visual Basic could help too. But, have in mind, we are not here to convert code. You should investigate PHP for your purpose.
<?php
class objSample {
private $_myPrivateVar = false;
public $myPublicProperty;
public function __get() {
return $this->_myPrivateVar;
}
public function __set($value) {
$this->_myPrivateVar = $value;
}
}
?>

Categories