calling methods in object oriented php - php

I'm sure this will look like stupid question for most of you. However, I've been banging my head for quite a while over it.
Coming from ASP.NET/C#, I'm trying to use PHP now. But the whole OOrintation gives me hard time.
I have the following code:
<html>
<head>
</head>
<body>
<?php
echo "hello<br/>";
class clsA
{
function a_func()
{
echo "a_func() executed <br/>";
}
}
abstract class clsB
{
protected $A;
function clsB()
{
$A = new clsA();
echo "clsB constructor ended<br/>";
}
}
class clsC extends clsB
{
function try_this()
{
echo "entered try_this() function <br/>";
$this->A->a_func();
}
}
$c = new clsC();
$c->try_this();
echo "end successfuly<br/>";
?>
</body>
</html>
To my simple understanding this code should result with the following lines:
hello
clsB constructor ended
entered try_this() function
a_func() executed
however, it does not run 'a_func', all I get is:
hello
clsB constructor ended
entered try_this() function
Can anyone spot the problem?
Thanks in advanced.

Your problem lies here:
$A = new clsA();
Here, you're assigning a new clsA object to the local variable $A. What you meant to do was assign it to the property $A:
$this->A = new clsA();

As the first answer but also you could extend the b class to the a class this way you can access the a class in C, like below:
<?php
echo "hello<br/>";
class clsA{
function a_func(){
echo "a_func() executed <br/>";
}
}
abstract class clsB extends clsA{
function clsB(){
echo "clsB constructor ended<br/>";
}
}
class clsC extends clsB{
function try_this(){
echo "entered try_this() function <br/>";
self::a_func();
}
}
$c = new clsC();
$c->try_this();
echo "end successfuly<br/>";
?>

Related

PHP Call a function from another function in same class

I am unable figure out how to make this work any help will be appreciated
<?php
class some{
function display()
{
$w ="its working";
$this->show($w);
}
function show($s)
{
echo $s;
}
}
?>
You were rightly advised to create an instance of your class then call the method on it but you said
see thats what i don't want .....i want some way to make it work without adding those two lines...by doing something else...just not that...and i can't figure out what i can do.
That something else is Simple! Make your method static
Declaring class properties or methods as static makes them accessible without needing an instantiation of the class.
public static function display()
{
$w ="its working";
self::show($w);
}
Then you can just do
some::display();
Fiddle
well it is working if you add the last two lines:
<?php
class some{
function display()
{
$w ="its working";
$this->show($w);
}
function show($s)
{
echo $s;
}
}
$x = new some;
$x->display();
?>
see here and click on "execute code"
Seems you are not called to display() function. Call to that function and try again.

PHP programing print constant value using scope resolution operator with class

I want to print constant value in PHP by scope resolution operator, but it is echo this : $p=new Ninja(); echo $p->show();
Please note that I have just started to learning so there could be mistakes....
<html>
<head>
<title>Scope it Out!</title>
</head>
<body>
<p>
<?php
class Person {
}
class Ninja extends Person
{
const stealt="MAXIMUM";
function show()
{
echo Ninja::stealth;
}
}
?>
$p=new Ninja();
echo $p->show();
</p>
</body>
</html>
Correct your spelling for stealth.
Ninja::stealth; should be self::stealth
NEVER put a class definition along with HTML. Create a separate class file and include it.
$p=new Ninja(); and echo $p->show(); are outside PHP tags.
class Person {
}
class Ninja extends Person
{
const stealt="MAXIMUM";
function show()
{
echo Ninja::stealt; // spelling mistake here..
}
}
//remove php closed tag from here...
$p=new Ninja();
echo $p->show();

Simple OOP PHP Question

Sorry for the noob question.
I have the following code which works fine:
$obj = new view;
echo $obj->connect();
echo $obj->content_two();
Basically what I need to do is get a variable called $variable from inside the function content_two(), How would I call $variable from content_two() ?
Thanks in advance.
With a member property.
<?php
class View {
protected $foo;
function connect() {
$this->foo = 42;
return "Hello from connect";
}
function connectTwo() {
return "Hello from connectTwo. foo = " . $this->foo;
}
}
Note that the conventions dictates that classes are named in CapitalCamelCase, whereas methods (functions on objects) and properties (variables on objects) are named in lowerCamelCase. You don't have to follow the convention, but it's probably a good idea to do so.

Run code on creation of a PHP class instance?

I want code to run whenever I create a new object. For example, see this:
<?php
class Test {
echo 'Hello, World!';
}
$test = new Test;
?>
I want it to echo "Hello, World!" whenever I create a new instance of this object, without calling a function afterward. Is this possible?
You should read about constructor
<?php
class MyClass {
public function __construct() {
echo "This code is executed when class in instanciated.";
}
}
?>
class Test {
function __construct(){
echo 'Hello, World!';
}
}
Or on PHP 4 use:
class Test {
function Test {
echo 'Hi';
}
}
Edit: This also works on PHP 5, so this is the best way to do it.

PHP syntax: constructing a class from a member variable

I am trying to make a class from a member variable like this:
<?
class A{
private $to_construct = 'B';
function make_class(){
// code to make class goes here
}
}
class B{
function __construct(){
echo 'class constructed';
}
}
$myA = new A();
$myA->make_class();
?>
I tried using:
$myClass = new $this->to_construct();
and
$myClass = new {$this->to_construct}();
but neither worked. I ended up having to do this:
$constructor = $this->to_construct;
$myClass = new $constructor();
It seems like there should be a way to do this without storing the class name in a local variable. Am I missing something?
Have you tried this?
$myClass = new $this->to_construct;
Are you using PHP 4 or something? On 5.2.9 $myClass = new $this->to_construct(); works perfectly.
In the end it's what you have to live with, with PHP. PHP syntax and semantics are VERY inconsistent. For example, an array access to the result of a call is a syntax error:
function foo() {
return array("foo","bar");
}
echo $foo()[0];
Any other language could do that but PHP can't. Sometimes you simply need to store values into local variables.
Same is true for func_get_args() in older versions of PHP. If you wanted to pass it to a function, you needed to store it in a local var first.
If I read well between the lines you are trying to do something like this. Right?
class createObject{
function __construct($class){
$this->$class=new $class;
}
}
class B{
function __construct(){
echo 'class B constructed<br>';
}
function sayHi(){
echo 'Hi I am class: '.get_class();
}
}
class C{
function __construct(){
echo 'class C constructed<br>';
}
function sayHi(){
echo 'Hi I am class: '.get_class();
}
}
$wantedClass='B';
$finalObject = new createObject($wantedClass);
$finalObject->$wantedClass->sayHi();
--
Dam

Categories